mirror of
https://github.com/microsoft/semantic-kernel.git
synced 2026-10-12 02:19:06 +00:00
Python: Fix notebooks 10 and 11 (#3557)
### Motivation and Context fix: #3514, I didn't know which integration tests @markwallace-microsoft is referring to but I fixed both notebooks by introducing a check before running the code that users have to define at the beginning of the notebook. ### Description Whichever service the user chooses (OpenAI - Azure OpenAI - Huggine Face) the code adjusts accordingly without the need to change anything later other than the .env file (obviously) and the Service name at the second cell of the two notebooks.
This commit is contained in:
@@ -1,362 +1,387 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"id": "68e1c158",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Multiple Results"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"id": "fb81bacd",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"In this notebook we show how you can in a single request, have the LLM model return multiple results per prompt. This is useful for running experiments where you want to evaluate the robustness of your prompt and the parameters of your config against a particular large language model."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "a77bdf89",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"!python -m pip install semantic-kernel==0.4.4.dev0"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "508ad44f",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import semantic_kernel as sk\n",
|
||||
"from semantic_kernel.connectors.ai.open_ai.request_settings.open_ai_request_settings import (\n",
|
||||
" OpenAITextRequestSettings,\n",
|
||||
" OpenAIChatRequestSettings,\n",
|
||||
")\n",
|
||||
"from semantic_kernel.connectors.ai.open_ai.request_settings.azure_chat_request_settings import AzureChatRequestSettings\n",
|
||||
"from semantic_kernel.connectors.ai.open_ai import (\n",
|
||||
" AzureTextCompletion,\n",
|
||||
" AzureChatCompletion,\n",
|
||||
" OpenAITextCompletion,\n",
|
||||
" OpenAIChatCompletion,\n",
|
||||
")\n",
|
||||
"from semantic_kernel.connectors.ai.hugging_face import HuggingFaceTextCompletion"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"id": "d8ddffc1",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"First, we will set up the text and chat services we will be submitting prompts to."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "8f8dcbc6",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"kernel = sk.Kernel()\n",
|
||||
"\n",
|
||||
"# Configure Azure LLM service\n",
|
||||
"deployment, api_key, endpoint = sk.azure_openai_settings_from_dot_env()\n",
|
||||
"azure_text_service = AzureTextCompletion(\n",
|
||||
" deployment_name=\"text-davinci-003\", endpoint=endpoint, api_key=api_key\n",
|
||||
") # set the deployment name to the value of your text model\n",
|
||||
"azure_chat_service = AzureChatCompletion(\n",
|
||||
" deployment_name=\"gpt-35-turbo\", endpoint=endpoint, api_key=api_key\n",
|
||||
") # set the deployment name to the value of your chat model\n",
|
||||
"\n",
|
||||
"# Configure OpenAI service\n",
|
||||
"api_key, org_id = sk.openai_settings_from_dot_env()\n",
|
||||
"oai_text_service = OpenAITextCompletion(ai_model_id=\"text-davinci-003\", api_key=api_key, org_id=org_id)\n",
|
||||
"oai_chat_service = OpenAIChatCompletion(ai_model_id=\"gpt-3.5-turbo\", api_key=api_key, org_id=org_id)\n",
|
||||
"\n",
|
||||
"# Configure Hugging Face service\n",
|
||||
"hf_text_service = HuggingFaceTextCompletion(ai_model_id=\"distilgpt2\", task=\"text-generation\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"id": "50561d82",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Next, we'll set up the completion request settings for text completion services."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "628c843e",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"oai_text_request_settings = OpenAITextRequestSettings(\n",
|
||||
" extension_data={\n",
|
||||
" \"max_tokens\": 80,\n",
|
||||
" \"temperature\": 0.7,\n",
|
||||
" \"top_p\": 1,\n",
|
||||
" \"frequency_penalty\": 0.5,\n",
|
||||
" \"presence_penalty\": 0.5,\n",
|
||||
" \"number_of_responses\": 3,\n",
|
||||
" }\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"id": "857a9c89",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Multiple Open AI Text Completions"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "e2979db8",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"prompt = \"what is the purpose of a rubber duck?\"\n",
|
||||
"results = await oai_text_service.complete_async(prompt=prompt, settings=oai_text_request_settings)\n",
|
||||
"i = 1\n",
|
||||
"for result in results:\n",
|
||||
" print(f\"Result {i}: {result}\")\n",
|
||||
" i += 1"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"id": "4288d09f",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Multiple Azure Open AI Text Completions"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "5319f14d",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"prompt = \"provide me a list of possible meanings for the acronym 'ORLD'\"\n",
|
||||
"results = await azure_text_service.complete_async(prompt=prompt, settings=oai_text_request_settings)\n",
|
||||
"i = 1\n",
|
||||
"for result in results:\n",
|
||||
" print(f\"Result {i}: {result}\")\n",
|
||||
" i += 1"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"id": "eb548f9c",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Multiple Hugging Face Text Completions"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "4a148709",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from semantic_kernel.connectors.ai.hugging_face.hf_request_settings import (\n",
|
||||
" HuggingFaceRequestSettings,\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"hf_request_settings = HuggingFaceRequestSettings(extension_data={\"max_new_tokens\": 80, \"temperature\": 0.7, \"top_p\": 1})"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "9525e4f3",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"prompt = \"The purpose of a rubber duck is\"\n",
|
||||
"results = await hf_text_service.complete_async(prompt=prompt, request_settings=hf_request_settings)\n",
|
||||
"print(\"\".join(results))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"id": "da632e12",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Here, we're setting up the settings for Chat completions."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "e5f11e46",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"oai_chat_request_settings = OpenAIChatRequestSettings(\n",
|
||||
" max_tokens=80, temperature=0.7, top_p=1, frequency_penalty=0.5, presence_penalty=0.5, number_of_responses=3\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"id": "d6bf238e",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Multiple OpenAI Chat Completions"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "dabc6a4c",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"role = \"user\"\n",
|
||||
"content = \"It's a beautiful day outside, birds are singing, flowers are blooming. On days like these, kids like you...\"\n",
|
||||
"message = {\"role\": role, \"content\": content}\n",
|
||||
"results = await oai_chat_service.complete_chat_async(messages=[message], settings=oai_chat_request_settings)\n",
|
||||
"i = 0\n",
|
||||
"for result in results:\n",
|
||||
" print(f\"Result {i}: {result[0]}\")\n",
|
||||
" i += 1"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"id": "cdb8f740",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Multiple Azure OpenAI Chat Completions"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "66ba4767",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"az_oai_request_settings = AzureChatRequestSettings(\n",
|
||||
" max_tokens=80, temperature=0.7, top_p=1, frequency_penalty=0.5, presence_penalty=0.5, number_of_responses=3\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "b74a64a9",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"role = \"user\"\n",
|
||||
"content = \"Tomorow is going to be a great day, I can feel it. I'm going to wake up early, go for a run, and then...\"\n",
|
||||
"message = {\"role\": role, \"content\": content}\n",
|
||||
"results = await azure_chat_service.complete_chat_async(messages=[message], settings=az_oai_request_settings)\n",
|
||||
"i = 0\n",
|
||||
"for result in results:\n",
|
||||
" print(f\"Result {i}: {result[0]}\")\n",
|
||||
" i += 1"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"id": "98c8191d",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Streaming Multiple Results\n",
|
||||
"\n",
|
||||
"Here is an example pattern if you want to stream your multiple results. Note that this is not supported for Hugging Face text completions at this time."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "26a37702",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import os\n",
|
||||
"from IPython.display import clear_output\n",
|
||||
"import time\n",
|
||||
"\n",
|
||||
"# Determine the clear command based on OS\n",
|
||||
"clear_command = \"cls\" if os.name == \"nt\" else \"clear\"\n",
|
||||
"\n",
|
||||
"prompt = \"what is the purpose of a rubber duck?\"\n",
|
||||
"stream = oai_text_service.complete_stream_async(prompt=prompt, settings=oai_text_request_settings)\n",
|
||||
"number_of_responses = oai_text_request_settings.number_of_responses\n",
|
||||
"texts = [\"\"] * number_of_responses\n",
|
||||
"\n",
|
||||
"last_clear_time = time.time()\n",
|
||||
"clear_interval = 0.5 # seconds\n",
|
||||
"\n",
|
||||
"# Note: there are some quirks with displaying the output, which sometimes flashes and disappears.\n",
|
||||
"# This could be influenced by a few factors specific to Jupyter notebooks and asynchronous processing.\n",
|
||||
"# The following code attempts to buffer the results to avoid the output flashing on/off the screen.\n",
|
||||
"\n",
|
||||
"async for results in stream:\n",
|
||||
" current_time = time.time()\n",
|
||||
"\n",
|
||||
" # Update texts with new results\n",
|
||||
" for idx, result in enumerate(results):\n",
|
||||
" if idx < number_of_responses:\n",
|
||||
" texts[idx] += result\n",
|
||||
"\n",
|
||||
" # Clear and display output at intervals\n",
|
||||
" if current_time - last_clear_time > clear_interval:\n",
|
||||
" clear_output(wait=True)\n",
|
||||
" for idx, text in enumerate(texts):\n",
|
||||
" print(f\"Result {idx + 1}: {text}\")\n",
|
||||
" last_clear_time = current_time\n",
|
||||
"\n",
|
||||
"print(\"----------------------------------------\")"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3 (ipykernel)",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.10.12"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
"cells": [
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"id": "68e1c158",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Multiple Results"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"id": "fb81bacd",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"In this notebook we show how you can in a single request, have the LLM model return multiple results per prompt. This is useful for running experiments where you want to evaluate the robustness of your prompt and the parameters of your config against a particular large language model."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "a77bdf89",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"!python -m pip install semantic-kernel==0.4.4.dev0"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "3f4bfee4",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from services import Service\n",
|
||||
"\n",
|
||||
"# Select a service to use for this notebook (available services: OpenAI, AzureOpenAI, HuggingFace)\n",
|
||||
"selectedService = Service.AzureOpenAI"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "508ad44f",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import semantic_kernel as sk\n",
|
||||
"if selectedService == Service.OpenAI or selectedService == Service.AzureOpenAI:\n",
|
||||
" from semantic_kernel.connectors.ai.open_ai.request_settings.open_ai_request_settings import (\n",
|
||||
" OpenAITextRequestSettings,\n",
|
||||
" OpenAIChatRequestSettings,\n",
|
||||
" )\n",
|
||||
" from semantic_kernel.connectors.ai.open_ai.request_settings.azure_chat_request_settings import AzureChatRequestSettings\n",
|
||||
" from semantic_kernel.connectors.ai.open_ai import (\n",
|
||||
" AzureTextCompletion,\n",
|
||||
" AzureChatCompletion,\n",
|
||||
" OpenAITextCompletion,\n",
|
||||
" OpenAIChatCompletion,\n",
|
||||
" )\n",
|
||||
"if selectedService == Service.HuggingFace:\n",
|
||||
" from semantic_kernel.connectors.ai.hugging_face import HuggingFaceTextCompletion"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"id": "d8ddffc1",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"First, we will set up the text and chat services we will be submitting prompts to."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "8f8dcbc6",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"kernel = sk.Kernel()\n",
|
||||
"\n",
|
||||
"# Configure Azure LLM service\n",
|
||||
"if selectedService == Service.AzureOpenAI:\n",
|
||||
" deployment, api_key, endpoint = sk.azure_openai_settings_from_dot_env()\n",
|
||||
" azure_text_service = AzureTextCompletion(\n",
|
||||
" deployment_name=\"gpt-35-turbo-instruct\", endpoint=endpoint, api_key=api_key\n",
|
||||
" ) # set the deployment name to the value of your text model (e.g. gpt-35-turbo-instruct or text-davinci-003)\n",
|
||||
" azure_chat_service = AzureChatCompletion(\n",
|
||||
" deployment_name=\"gpt-35-turbo\", endpoint=endpoint, api_key=api_key\n",
|
||||
" ) # set the deployment name to the value of your chat model\n",
|
||||
"\n",
|
||||
"# Configure OpenAI service\n",
|
||||
"if selectedService == Service.OpenAI:\n",
|
||||
" api_key, org_id = sk.openai_settings_from_dot_env()\n",
|
||||
" oai_text_service = OpenAITextCompletion(ai_model_id=\"gpt-35-turbo-instruct\", api_key=api_key, org_id=org_id)\n",
|
||||
" oai_chat_service = OpenAIChatCompletion(ai_model_id=\"gpt-3.5-turbo\", api_key=api_key, org_id=org_id)\n",
|
||||
"\n",
|
||||
"# Configure Hugging Face service\n",
|
||||
"if selectedService == Service.HuggingFace:\n",
|
||||
" hf_text_service = HuggingFaceTextCompletion(ai_model_id=\"distilgpt2\", task=\"text-generation\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"id": "50561d82",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Next, we'll set up the completion request settings for text completion services."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "628c843e",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"oai_text_request_settings = OpenAITextRequestSettings(\n",
|
||||
" extension_data={\n",
|
||||
" \"max_tokens\": 80,\n",
|
||||
" \"temperature\": 0.7,\n",
|
||||
" \"top_p\": 1,\n",
|
||||
" \"frequency_penalty\": 0.5,\n",
|
||||
" \"presence_penalty\": 0.5,\n",
|
||||
" \"number_of_responses\": 3,\n",
|
||||
" }\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"id": "857a9c89",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Multiple Open AI Text Completions"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "e2979db8",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"if selectedService == Service.OpenAI:\n",
|
||||
" prompt = \"what is the purpose of a rubber duck?\"\n",
|
||||
" results = await oai_text_service.complete_async(prompt=prompt, settings=oai_text_request_settings)\n",
|
||||
" i = 1\n",
|
||||
" for result in results:\n",
|
||||
" print(f\"Result {i}: {result}\")\n",
|
||||
" i += 1"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"id": "4288d09f",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Multiple Azure Open AI Text Completions"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "5319f14d",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"if selectedService == Service.AzureOpenAI:\n",
|
||||
" prompt = \"provide me a list of possible meanings for the acronym 'ORLD'\"\n",
|
||||
" results = await azure_text_service.complete_async(prompt=prompt, settings=oai_text_request_settings)\n",
|
||||
" i = 1\n",
|
||||
" for result in results:\n",
|
||||
" print(f\"Result {i}: {result}\")\n",
|
||||
" i += 1"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"id": "eb548f9c",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Multiple Hugging Face Text Completions"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "4a148709",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"if selectedService == Service.HuggingFace:\n",
|
||||
" from semantic_kernel.connectors.ai.hugging_face.hf_request_settings import (\n",
|
||||
" HuggingFaceRequestSettings,\n",
|
||||
" )\n",
|
||||
"\n",
|
||||
" hf_request_settings = HuggingFaceRequestSettings(extension_data={\"max_new_tokens\": 80, \"temperature\": 0.7, \"top_p\": 1})"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "9525e4f3",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"if selectedService == Service.HuggingFace:\n",
|
||||
" prompt = \"The purpose of a rubber duck is\"\n",
|
||||
" results = await hf_text_service.complete_async(prompt=prompt, request_settings=hf_request_settings)\n",
|
||||
" print(\"\".join(results))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"id": "da632e12",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Here, we're setting up the settings for Chat completions."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "e5f11e46",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"oai_chat_request_settings = OpenAIChatRequestSettings(\n",
|
||||
" max_tokens=80, temperature=0.7, top_p=1, frequency_penalty=0.5, presence_penalty=0.5, number_of_responses=3\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"id": "d6bf238e",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Multiple OpenAI Chat Completions"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "dabc6a4c",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"if selectedService == Service.OpenAI:\n",
|
||||
" role = \"user\"\n",
|
||||
" content = \"It's a beautiful day outside, birds are singing, flowers are blooming. On days like these, kids like you...\"\n",
|
||||
" message = {\"role\": role, \"content\": content}\n",
|
||||
" results = await oai_chat_service.complete_chat_async(messages=[message], settings=oai_chat_request_settings)\n",
|
||||
" i = 0\n",
|
||||
" for result in results:\n",
|
||||
" print(f\"Result {i}: {result[0]}\")\n",
|
||||
" i += 1"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"id": "cdb8f740",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Multiple Azure OpenAI Chat Completions"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "66ba4767",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"az_oai_request_settings = AzureChatRequestSettings(\n",
|
||||
" max_tokens=80, temperature=0.7, top_p=1, frequency_penalty=0.5, presence_penalty=0.5, number_of_responses=3\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "b74a64a9",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"if selectedService == Service.AzureOpenAI:\n",
|
||||
" role = \"user\"\n",
|
||||
" content = \"Tomorow is going to be a great day, I can feel it. I'm going to wake up early, go for a run, and then...\"\n",
|
||||
" message = {\"role\": role, \"content\": content}\n",
|
||||
" results = await azure_chat_service.complete_chat_async(messages=[message], settings=az_oai_request_settings)\n",
|
||||
" i = 0\n",
|
||||
" for result in results:\n",
|
||||
" print(f\"Result {i}: {result[0]}\")\n",
|
||||
" i += 1"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"id": "98c8191d",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Streaming Multiple Results\n",
|
||||
"\n",
|
||||
"Here is an example pattern if you want to stream your multiple results. Note that this is not supported for Hugging Face text completions at this time."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "26a37702",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"if selectedService == Service.OpenAI:\n",
|
||||
" import os\n",
|
||||
" from IPython.display import clear_output\n",
|
||||
" import time\n",
|
||||
"\n",
|
||||
" # Determine the clear command based on OS\n",
|
||||
" clear_command = \"cls\" if os.name == \"nt\" else \"clear\"\n",
|
||||
"\n",
|
||||
" prompt = \"what is the purpose of a rubber duck?\"\n",
|
||||
" stream = oai_text_service.complete_stream_async(prompt=prompt, settings=oai_text_request_settings)\n",
|
||||
" number_of_responses = oai_text_request_settings.number_of_responses\n",
|
||||
" texts = [\"\"] * number_of_responses\n",
|
||||
"\n",
|
||||
" last_clear_time = time.time()\n",
|
||||
" clear_interval = 0.5 # seconds\n",
|
||||
"\n",
|
||||
" # Note: there are some quirks with displaying the output, which sometimes flashes and disappears.\n",
|
||||
" # This could be influenced by a few factors specific to Jupyter notebooks and asynchronous processing.\n",
|
||||
" # The following code attempts to buffer the results to avoid the output flashing on/off the screen.\n",
|
||||
"\n",
|
||||
" async for results in stream:\n",
|
||||
" current_time = time.time()\n",
|
||||
"\n",
|
||||
" # Update texts with new results\n",
|
||||
" for idx, result in enumerate(results):\n",
|
||||
" if idx < number_of_responses:\n",
|
||||
" texts[idx] += result\n",
|
||||
"\n",
|
||||
" # Clear and display output at intervals\n",
|
||||
" if current_time - last_clear_time > clear_interval:\n",
|
||||
" clear_output(wait=True)\n",
|
||||
" for idx, text in enumerate(texts):\n",
|
||||
" print(f\"Result {idx + 1}: {text}\")\n",
|
||||
" last_clear_time = current_time\n",
|
||||
"\n",
|
||||
" print(\"----------------------------------------\")"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3 (ipykernel)",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.10.12"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
|
||||
@@ -1,305 +1,329 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"id": "68e1c158",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Streaming Results"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "a77bdf89",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"!python -m pip install semantic-kernel==0.4.4.dev0"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "508ad44f",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import semantic_kernel as sk\n",
|
||||
"from semantic_kernel.connectors.ai.open_ai.request_settings.open_ai_request_settings import (\n",
|
||||
" OpenAITextRequestSettings,\n",
|
||||
" OpenAIChatRequestSettings,\n",
|
||||
")\n",
|
||||
"from semantic_kernel.connectors.ai.open_ai.request_settings.azure_chat_request_settings import (\n",
|
||||
" AzureChatRequestSettings,\n",
|
||||
")\n",
|
||||
"from semantic_kernel.connectors.ai.open_ai import (\n",
|
||||
" AzureTextCompletion,\n",
|
||||
" AzureChatCompletion,\n",
|
||||
" OpenAITextCompletion,\n",
|
||||
" OpenAIChatCompletion,\n",
|
||||
")\n",
|
||||
"from semantic_kernel.connectors.ai.hugging_face import HuggingFaceTextCompletion"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"id": "d8ddffc1",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"First, we will set up the text and chat services we will be submitting prompts to."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "8f8dcbc6",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"kernel = sk.Kernel()\n",
|
||||
"\n",
|
||||
"# Configure Azure LLM service\n",
|
||||
"deployment, api_key, endpoint = sk.azure_openai_settings_from_dot_env()\n",
|
||||
"azure_text_service = AzureTextCompletion(\n",
|
||||
" deployment_name=\"text\", endpoint=endpoint, api_key=api_key\n",
|
||||
") # set the deployment name to the value of your text model\n",
|
||||
"azure_chat_service = AzureChatCompletion(\n",
|
||||
" deployment_name=\"turbo\", endpoint=endpoint, api_key=api_key\n",
|
||||
") # set the deployment name to the value of your chat model\n",
|
||||
"\n",
|
||||
"# Configure OpenAI service\n",
|
||||
"api_key, org_id = sk.openai_settings_from_dot_env()\n",
|
||||
"oai_text_service = OpenAITextCompletion(ai_model_id=\"text-davinci-003\", api_key=api_key, org_id=org_id)\n",
|
||||
"oai_chat_service = OpenAIChatCompletion(ai_model_id=\"gpt-3.5-turbo\", api_key=api_key, org_id=org_id)\n",
|
||||
"\n",
|
||||
"# Configure Hugging Face service\n",
|
||||
"hf_text_service = HuggingFaceTextCompletion(ai_model_id=\"distilgpt2\", task=\"text-generation\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"id": "50561d82",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Next, we'll set up the completion request settings for text completion services."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "628c843e",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"oai_request_settings = OpenAITextRequestSettings(\n",
|
||||
" max_tokens=150,\n",
|
||||
" temperature=0.7,\n",
|
||||
" top_p=1,\n",
|
||||
" frequency_penalty=0.5,\n",
|
||||
" presence_penalty=0.5,\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"id": "857a9c89",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Streaming Open AI Text Completion"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "e2979db8",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"prompt = \"what is the purpose of a rubber duck?\"\n",
|
||||
"stream = oai_text_service.complete_stream_async(prompt=prompt, settings=oai_request_settings)\n",
|
||||
"async for text in stream:\n",
|
||||
" print(text, end=\"\") # end = \"\" to avoid newlines"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"id": "4288d09f",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Streaming Azure Open AI Text Completion"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "5319f14d",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"prompt = \"provide me a list of possible meanings for the acronym 'ORLD'\"\n",
|
||||
"stream = azure_text_service.complete_stream_async(prompt=prompt, settings=oai_request_settings)\n",
|
||||
"async for text in stream:\n",
|
||||
" print(text, end=\"\") # end = \"\" to avoid newlines"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"id": "eb548f9c",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Streaming Hugging Face Text Completion"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "be7b1c2e",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from semantic_kernel.connectors.ai.hugging_face.hf_request_settings import (\n",
|
||||
" HuggingFaceRequestSettings,\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"hf_request_settings = HuggingFaceRequestSettings(\n",
|
||||
" extension_data={\n",
|
||||
" \"max_new_tokens\": 80,\n",
|
||||
" \"top_p\": 1,\n",
|
||||
" \"eos_token_id\": 11,\n",
|
||||
" \"pad_token_id\": 0,\n",
|
||||
" }\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "9525e4f3",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"prompt = \"The purpose of a rubber duck is\"\n",
|
||||
"stream = hf_text_service.complete_stream_async(prompt=prompt, request_settings=hf_request_settings)\n",
|
||||
"async for text in stream:\n",
|
||||
" print(text, end=\"\") # end = \"\" to avoid newlines"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"id": "da632e12",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Here, we're setting up the settings for Chat completions."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "e5f11e46",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"oai_chat_request_settings = OpenAIChatRequestSettings(\n",
|
||||
" max_tokens=150,\n",
|
||||
" temperature=0.7,\n",
|
||||
" top_p=1,\n",
|
||||
" frequency_penalty=0.5,\n",
|
||||
" presence_penalty=0.5,\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"id": "d6bf238e",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Streaming OpenAI Chat Completion"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "dabc6a4c",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"role = \"system\"\n",
|
||||
"content = \"You are an AI assistant that helps people find information.\"\n",
|
||||
"message = {\"role\": role, \"content\": content}\n",
|
||||
"stream = oai_chat_service.complete_chat_stream_async(messages=[message], settings=oai_chat_request_settings)\n",
|
||||
"async for text in stream:\n",
|
||||
" print(text, end=\"\") # end = \"\" to avoid newlines"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"id": "cdb8f740",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Streaming Azure OpenAI Chat Completion"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "da1e9f59",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"az_oai_chat_request_settings = AzureChatRequestSettings(\n",
|
||||
" max_tokens=150,\n",
|
||||
" temperature=0.7,\n",
|
||||
" top_p=1,\n",
|
||||
" frequency_penalty=0.5,\n",
|
||||
" presence_penalty=0.5,\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "b74a64a9",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"role = \"system\"\n",
|
||||
"content = \"You are an AI assistant that helps people find information.\"\n",
|
||||
"message = {\"role\": role, \"content\": content}\n",
|
||||
"stream = azure_chat_service.complete_chat_stream_async(messages=[message], settings=az_oai_chat_request_settings)\n",
|
||||
"async for text in stream:\n",
|
||||
" print(text, end=\"\") # end = \"\" to avoid newlines"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3 (ipykernel)",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.10.12"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
"cells": [
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"id": "68e1c158",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Streaming Results\n",
|
||||
"Here is an example pattern if you want to stream your multiple results. Note that this is not supported for Hugging Face text completions at this time."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "a77bdf89",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"!python -m pip install semantic-kernel==0.4.4.dev0"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from services import Service\n",
|
||||
"\n",
|
||||
"# Select a service to use for this notebook (available services: OpenAI, AzureOpenAI, HuggingFace)\n",
|
||||
"selectedService = Service.AzureOpenAI"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "508ad44f",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import semantic_kernel as sk\n",
|
||||
"if selectedService == Service.OpenAI or selectedService == Service.AzureOpenAI:\n",
|
||||
" from semantic_kernel.connectors.ai.open_ai.request_settings.open_ai_request_settings import (\n",
|
||||
" OpenAITextRequestSettings,\n",
|
||||
" OpenAIChatRequestSettings,\n",
|
||||
" )\n",
|
||||
" from semantic_kernel.connectors.ai.open_ai.request_settings.azure_chat_request_settings import (\n",
|
||||
" AzureChatRequestSettings,\n",
|
||||
" )\n",
|
||||
" from semantic_kernel.connectors.ai.open_ai import (\n",
|
||||
" AzureTextCompletion,\n",
|
||||
" AzureChatCompletion,\n",
|
||||
" OpenAITextCompletion,\n",
|
||||
" OpenAIChatCompletion,\n",
|
||||
" )\n",
|
||||
"if selectedService == Service.HuggingFace:\n",
|
||||
" from semantic_kernel.connectors.ai.hugging_face import HuggingFaceTextCompletion"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"id": "d8ddffc1",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"First, we will set up the text and chat services we will be submitting prompts to."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "8f8dcbc6",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"kernel = sk.Kernel()\n",
|
||||
"\n",
|
||||
"# Configure Azure LLM service\n",
|
||||
"if selectedService == Service.AzureOpenAI:\n",
|
||||
" deployment, api_key, endpoint = sk.azure_openai_settings_from_dot_env()\n",
|
||||
" azure_text_service = AzureTextCompletion(\n",
|
||||
" deployment_name=\"gpt-35-turbo-instruct\", endpoint=endpoint, api_key=api_key\n",
|
||||
" ) # set the deployment name to the value of your text model (e.g. gpt-35-turbo-instruct or text-davinci-003)\n",
|
||||
" azure_chat_service = AzureChatCompletion(\n",
|
||||
" deployment_name=\"gpt-35-turbo\", endpoint=endpoint, api_key=api_key\n",
|
||||
" ) # set the deployment name to the value of your chat model\n",
|
||||
"\n",
|
||||
"# Configure OpenAI service\n",
|
||||
"if selectedService == Service.OpenAI:\n",
|
||||
" api_key, org_id = sk.openai_settings_from_dot_env()\n",
|
||||
" oai_text_service = OpenAITextCompletion(ai_model_id=\"gpt-35-turbo-instruct\", api_key=api_key, org_id=org_id)\n",
|
||||
" oai_chat_service = OpenAIChatCompletion(ai_model_id=\"gpt-3.5-turbo\", api_key=api_key, org_id=org_id)\n",
|
||||
"\n",
|
||||
"# Configure Hugging Face service\n",
|
||||
"if selectedService == Service.HuggingFace:\n",
|
||||
" hf_text_service = HuggingFaceTextCompletion(ai_model_id=\"distilgpt2\", task=\"text-generation\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"id": "50561d82",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Next, we'll set up the completion request settings for text completion services."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "628c843e",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"oai_request_settings = OpenAITextRequestSettings(\n",
|
||||
" max_tokens=150,\n",
|
||||
" temperature=0.7,\n",
|
||||
" top_p=1,\n",
|
||||
" frequency_penalty=0.5,\n",
|
||||
" presence_penalty=0.5,\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"id": "857a9c89",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Streaming Open AI Text Completion"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "e2979db8",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"if selectedService == Service.OpenAI:\n",
|
||||
" prompt = \"what is the purpose of a rubber duck?\"\n",
|
||||
" stream = oai_text_service.complete_stream_async(prompt=prompt, settings=oai_request_settings)\n",
|
||||
" async for text in stream:\n",
|
||||
" print(text, end=\"\") # end = \"\" to avoid newlines"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"id": "4288d09f",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Streaming Azure Open AI Text Completion"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "5319f14d",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"if selectedService == Service.AzureOpenAI:\n",
|
||||
" prompt = \"provide me a list of possible meanings for the acronym 'ORLD'\"\n",
|
||||
" stream = azure_text_service.complete_stream_async(prompt=prompt, settings=oai_request_settings)\n",
|
||||
" async for text in stream:\n",
|
||||
" print(text, end=\"\") # end = \"\" to avoid newlines"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"id": "eb548f9c",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Streaming Hugging Face Text Completion"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "be7b1c2e",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"if selectedService == Service.HuggingFace:\n",
|
||||
" from semantic_kernel.connectors.ai.hugging_face.hf_request_settings import (\n",
|
||||
" HuggingFaceRequestSettings,\n",
|
||||
" )\n",
|
||||
"\n",
|
||||
" hf_request_settings = HuggingFaceRequestSettings(\n",
|
||||
" extension_data={\n",
|
||||
" \"max_new_tokens\": 80,\n",
|
||||
" \"top_p\": 1,\n",
|
||||
" \"eos_token_id\": 11,\n",
|
||||
" \"pad_token_id\": 0,\n",
|
||||
" }\n",
|
||||
" )"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "9525e4f3",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"if selectedService == Service.HuggingFace:\n",
|
||||
" prompt = \"The purpose of a rubber duck is\"\n",
|
||||
" stream = hf_text_service.complete_stream_async(prompt=prompt, request_settings=hf_request_settings)\n",
|
||||
" async for text in stream:\n",
|
||||
" print(text, end=\"\") # end = \"\" to avoid newlines"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"id": "da632e12",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Here, we're setting up the settings for Chat completions."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "e5f11e46",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"oai_chat_request_settings = OpenAIChatRequestSettings(\n",
|
||||
" max_tokens=150,\n",
|
||||
" temperature=0.7,\n",
|
||||
" top_p=1,\n",
|
||||
" frequency_penalty=0.5,\n",
|
||||
" presence_penalty=0.5,\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"id": "d6bf238e",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Streaming OpenAI Chat Completion"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "dabc6a4c",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"if selectedService == Service.OpenAI:\n",
|
||||
" role = \"system\"\n",
|
||||
" content = \"You are an AI assistant that helps people find information.\"\n",
|
||||
" message = {\"role\": role, \"content\": content}\n",
|
||||
" stream = oai_chat_service.complete_chat_stream_async(messages=[message], settings=oai_chat_request_settings)\n",
|
||||
" async for text in stream:\n",
|
||||
" print(text, end=\"\") # end = \"\" to avoid newlines"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"id": "cdb8f740",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Streaming Azure OpenAI Chat Completion"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "da1e9f59",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"az_oai_chat_request_settings = AzureChatRequestSettings(\n",
|
||||
" max_tokens=150,\n",
|
||||
" temperature=0.7,\n",
|
||||
" top_p=1,\n",
|
||||
" frequency_penalty=0.5,\n",
|
||||
" presence_penalty=0.5,\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "b74a64a9",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"if selectedService == Service.AzureOpenAI:\n",
|
||||
" role = \"system\"\n",
|
||||
" content = \"You are an AI assistant that helps people find information.\"\n",
|
||||
" message = {\"role\": role, \"content\": content}\n",
|
||||
" stream = azure_chat_service.complete_chat_stream_async(messages=[message], settings=az_oai_chat_request_settings)\n",
|
||||
" async for text in stream:\n",
|
||||
" print(text, end=\"\") # end = \"\" to avoid newlines"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3 (ipykernel)",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.8.0"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
"""
|
||||
This module defines an enumeration representing different services.
|
||||
"""
|
||||
|
||||
from enum import Enum
|
||||
|
||||
|
||||
class Service(Enum):
|
||||
"""
|
||||
Attributes:
|
||||
OpenAI (str): Represents the OpenAI service.
|
||||
AzureOpenAI (str): Represents the Azure OpenAI service.
|
||||
HuggingFace (str): Represents the HuggingFace service.
|
||||
"""
|
||||
|
||||
OpenAI = "openai"
|
||||
AzureOpenAI = "azureopenai"
|
||||
HuggingFace = "huggingface"
|
||||
Reference in New Issue
Block a user