mirror of
https://github.com/microsoft/semantic-kernel.git
synced 2026-10-12 02:19:06 +00:00
Python: renamed complete to get_ (#6288)
### Motivation and Context <!-- Thank you for your contribution to the semantic-kernel repo! Please help reviewers and future users, providing the following information: 1. Why is this change required? 2. What problem does it solve? 3. What scenario does it contribute to? 4. If it fixes an open issue, please link to the issue here. --> To get back in sync with dotnet, renamed: - complete to get_text_contents - complete_chat to get_chat_message_contents - complete_stream to get_streaming_text_contents - complete_chat_stream to get_streaming_chat_message_content ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [x] The code builds clean without any errors or warnings - [x] The PR follows the [SK Contribution Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [x] All unit tests pass, and I have added new tests where possible - [ ] I didn't break anyone 😄 Co-authored-by: Evan Mattson <[email protected]>
This commit is contained in:
co-authored by
Evan Mattson
parent
e5a29dae8c
commit
cc7f7d2f69
@@ -116,7 +116,7 @@ async def main():
|
||||
|
||||
while True:
|
||||
# The result is a list of ChatMessageContent objects, grab the first one
|
||||
result = await chat.complete_chat(chat_history=chat_history, settings=settings)
|
||||
result = await chat.get_chat_message_contents(chat_history=chat_history, settings=settings)
|
||||
result = result[0]
|
||||
|
||||
if result.content:
|
||||
|
||||
@@ -12,7 +12,7 @@ async def text_completion_example_complete(kernel, user_mssg, settings):
|
||||
"""
|
||||
palm_text_completion = GooglePalmTextCompletion("models/text-bison-001")
|
||||
kernel.add_service(palm_text_completion)
|
||||
answer = await palm_text_completion.complete(user_mssg, settings)
|
||||
answer = await palm_text_completion.get_text_contents(user_mssg, settings)
|
||||
return answer
|
||||
|
||||
|
||||
|
||||
@@ -1,410 +1,420 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"id": "68e1c158",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Multiple Results\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"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.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "a77bdf89",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"!python -m pip install semantic-kernel==0.9.8b1"
|
||||
]
|
||||
},
|
||||
{
|
||||
"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.OpenAI"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "508ad44f",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from semantic_kernel.contents import ChatHistory # noqa: F401\n",
|
||||
"\n",
|
||||
"if selectedService == Service.OpenAI or selectedService == Service.AzureOpenAI:\n",
|
||||
" from semantic_kernel.connectors.ai.open_ai import ( # noqa: F401\n",
|
||||
" AzureChatCompletion,\n",
|
||||
" AzureChatPromptExecutionSettings,\n",
|
||||
" AzureTextCompletion,\n",
|
||||
" OpenAIChatCompletion,\n",
|
||||
" OpenAIChatPromptExecutionSettings,\n",
|
||||
" OpenAITextCompletion,\n",
|
||||
" OpenAITextPromptExecutionSettings,\n",
|
||||
" )\n",
|
||||
"if selectedService == Service.HuggingFace:\n",
|
||||
" from semantic_kernel.connectors.ai.hugging_face import HuggingFaceTextCompletion # noqa: F401"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"id": "d8ddffc1",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"First, we will set up the text and chat services we will be submitting prompts to.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "8f8dcbc6",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from semantic_kernel import Kernel\n",
|
||||
"\n",
|
||||
"kernel = Kernel()\n",
|
||||
"\n",
|
||||
"# Configure Azure LLM service\n",
|
||||
"if selectedService == Service.AzureOpenAI:\n",
|
||||
" azure_text_service = AzureTextCompletion(\n",
|
||||
" service_id=\"aoai_text\"\n",
|
||||
" ) # set the deployment name to the value of your text model (e.g. gpt-35-turbo-instruct)\n",
|
||||
" azure_chat_service = AzureChatCompletion(\n",
|
||||
" service_id=\"aoai_chat\"\n",
|
||||
" ) # set the deployment name to the value of your chat model\n",
|
||||
"\n",
|
||||
"# Configure OpenAI service\n",
|
||||
"if selectedService == Service.OpenAI:\n",
|
||||
" oai_text_service = OpenAITextCompletion(service_id=\"oai_text\", ai_model_id=\"gpt-3.5-turbo-instruct\")\n",
|
||||
" oai_chat_service = OpenAIChatCompletion(service_id=\"oai_chat\", ai_model_id=\"gpt-3.5-turbo\")\n",
|
||||
"\n",
|
||||
"# Configure Hugging Face service\n",
|
||||
"if selectedService == Service.HuggingFace:\n",
|
||||
" hf_text_service = HuggingFaceTextCompletion(service_id=\"hf_text\", 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.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "628c843e",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"oai_text_prompt_execution_settings = OpenAITextPromptExecutionSettings(\n",
|
||||
" service=\"oai_text\",\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\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"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",
|
||||
"\n",
|
||||
" results = await oai_text_service.complete(prompt=prompt, settings=oai_text_prompt_execution_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\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"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",
|
||||
"\n",
|
||||
" results = await azure_text_service.complete(prompt=prompt, settings=oai_text_prompt_execution_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\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "4a148709",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"if selectedService == Service.HuggingFace:\n",
|
||||
" from semantic_kernel.connectors.ai.hugging_face.hf_prompt_execution_settings import (\n",
|
||||
" HuggingFacePromptExecutionSettings,\n",
|
||||
" )\n",
|
||||
"\n",
|
||||
" hf_prompt_execution_settings = HuggingFacePromptExecutionSettings(\n",
|
||||
" service_id=\"hf_text\", extension_data={\"max_new_tokens\": 80, \"temperature\": 0.7, \"top_p\": 1}\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",
|
||||
"\n",
|
||||
" results = await hf_text_service.complete(prompt=prompt, prompt_execution_settings=hf_prompt_execution_settings)\n",
|
||||
" print(\"\".join(results))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"id": "da632e12",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Here, we're setting up the settings for Chat completions.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "e5f11e46",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"oai_chat_prompt_execution_settings = OpenAIChatPromptExecutionSettings(\n",
|
||||
" service_id=\"oai_chat\",\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",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"id": "d6bf238e",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Multiple OpenAI Chat Completions\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "dabc6a4c",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"if selectedService == Service.OpenAI:\n",
|
||||
" chat = ChatHistory()\n",
|
||||
" chat.add_user_message(\n",
|
||||
" \"It's a beautiful day outside, birds are singing, flowers are blooming. On days like these, kids like you...\"\n",
|
||||
" )\n",
|
||||
" results = await oai_chat_service.complete_chat(chat_history=chat, settings=oai_chat_prompt_execution_settings)\n",
|
||||
" i = 0\n",
|
||||
" for result in results:\n",
|
||||
" print(f\"Result {i+1}: {str(result)}\")\n",
|
||||
" i += 1"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"id": "cdb8f740",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Multiple Azure OpenAI Chat Completions\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "66ba4767",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"az_oai_prompt_execution_settings = AzureChatPromptExecutionSettings(\n",
|
||||
" service_id=\"aoai_chat\",\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",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "b74a64a9",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"if selectedService == Service.AzureOpenAI:\n",
|
||||
" content = (\n",
|
||||
" \"Tomorrow 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",
|
||||
" )\n",
|
||||
" chat = ChatHistory()\n",
|
||||
" chat.add_user_message(content)\n",
|
||||
" results = await azure_chat_service.complete_chat(chat_history=chat, settings=az_oai_prompt_execution_settings)\n",
|
||||
" i = 0\n",
|
||||
" for result in results:\n",
|
||||
" print(f\"Result {i+1}: {str(result)}\")\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.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "26a37702",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"if selectedService == Service.OpenAI:\n",
|
||||
" import os\n",
|
||||
" import time\n",
|
||||
"\n",
|
||||
" from IPython.display import clear_output\n",
|
||||
"\n",
|
||||
" # Determine the clear command based on OS\n",
|
||||
" clear_command = \"cls\" if os.name == \"nt\" else \"clear\"\n",
|
||||
"\n",
|
||||
" chat = ChatHistory()\n",
|
||||
" chat.add_user_message(\"what is the purpose of a rubber duck?\")\n",
|
||||
"\n",
|
||||
" stream = oai_text_service.complete_chat_stream(chat_history=chat, settings=oai_text_prompt_execution_settings)\n",
|
||||
" number_of_responses = oai_text_prompt_execution_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] += str(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.12.3"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
"cells": [
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"id": "68e1c158",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Multiple Results\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"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.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "a77bdf89",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"!python -m pip install semantic-kernel==0.9.8b1"
|
||||
]
|
||||
},
|
||||
{
|
||||
"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.OpenAI"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "508ad44f",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from semantic_kernel.contents import ChatHistory # noqa: F401\n",
|
||||
"\n",
|
||||
"if selectedService == Service.OpenAI or selectedService == Service.AzureOpenAI:\n",
|
||||
" from semantic_kernel.connectors.ai.open_ai import ( # noqa: F401\n",
|
||||
" AzureChatCompletion,\n",
|
||||
" AzureChatPromptExecutionSettings,\n",
|
||||
" AzureTextCompletion,\n",
|
||||
" OpenAIChatCompletion,\n",
|
||||
" OpenAIChatPromptExecutionSettings,\n",
|
||||
" OpenAITextCompletion,\n",
|
||||
" OpenAITextPromptExecutionSettings,\n",
|
||||
" )\n",
|
||||
"if selectedService == Service.HuggingFace:\n",
|
||||
" from semantic_kernel.connectors.ai.hugging_face import HuggingFaceTextCompletion # noqa: F401"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"id": "d8ddffc1",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"First, we will set up the text and chat services we will be submitting prompts to.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "8f8dcbc6",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from semantic_kernel import Kernel\n",
|
||||
"\n",
|
||||
"kernel = Kernel()\n",
|
||||
"\n",
|
||||
"# Configure Azure LLM service\n",
|
||||
"if selectedService == Service.AzureOpenAI:\n",
|
||||
" azure_text_service = AzureTextCompletion(\n",
|
||||
" service_id=\"aoai_text\"\n",
|
||||
" ) # set the deployment name to the value of your text model (e.g. gpt-35-turbo-instruct)\n",
|
||||
" azure_chat_service = AzureChatCompletion(\n",
|
||||
" service_id=\"aoai_chat\"\n",
|
||||
" ) # set the deployment name to the value of your chat model\n",
|
||||
"\n",
|
||||
"# Configure OpenAI service\n",
|
||||
"if selectedService == Service.OpenAI:\n",
|
||||
" oai_text_service = OpenAITextCompletion(service_id=\"oai_text\", ai_model_id=\"gpt-3.5-turbo-instruct\")\n",
|
||||
" oai_chat_service = OpenAIChatCompletion(service_id=\"oai_chat\", ai_model_id=\"gpt-3.5-turbo\")\n",
|
||||
"\n",
|
||||
"# Configure Hugging Face service\n",
|
||||
"if selectedService == Service.HuggingFace:\n",
|
||||
" hf_text_service = HuggingFaceTextCompletion(service_id=\"hf_text\", 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.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "628c843e",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"oai_text_prompt_execution_settings = OpenAITextPromptExecutionSettings(\n",
|
||||
" service=\"oai_text\",\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\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"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",
|
||||
"\n",
|
||||
" results = await oai_text_service.get_text_contents_contents(\n",
|
||||
" prompt=prompt, settings=oai_text_prompt_execution_settings\n",
|
||||
" )\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\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"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",
|
||||
"\n",
|
||||
" results = await azure_text_service.get_text_contents(prompt=prompt, settings=oai_text_prompt_execution_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\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "4a148709",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"if selectedService == Service.HuggingFace:\n",
|
||||
" from semantic_kernel.connectors.ai.hugging_face.hf_prompt_execution_settings import (\n",
|
||||
" HuggingFacePromptExecutionSettings,\n",
|
||||
" )\n",
|
||||
"\n",
|
||||
" hf_prompt_execution_settings = HuggingFacePromptExecutionSettings(\n",
|
||||
" service_id=\"hf_text\", extension_data={\"max_new_tokens\": 80, \"temperature\": 0.7, \"top_p\": 1}\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",
|
||||
"\n",
|
||||
" results = await hf_text_service.get_text_contents(\n",
|
||||
" prompt=prompt, prompt_execution_settings=hf_prompt_execution_settings\n",
|
||||
" )\n",
|
||||
" print(\"\".join(results))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"id": "da632e12",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Here, we're setting up the settings for Chat completions.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "e5f11e46",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"oai_chat_prompt_execution_settings = OpenAIChatPromptExecutionSettings(\n",
|
||||
" service_id=\"oai_chat\",\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",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"id": "d6bf238e",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Multiple OpenAI Chat Completions\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "dabc6a4c",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"if selectedService == Service.OpenAI:\n",
|
||||
" chat = ChatHistory()\n",
|
||||
" chat.add_user_message(\n",
|
||||
" \"It's a beautiful day outside, birds are singing, flowers are blooming. On days like these, kids like you...\"\n",
|
||||
" )\n",
|
||||
" results = await oai_chat_service.get_chat_message_contents(\n",
|
||||
" chat_history=chat, settings=oai_chat_prompt_execution_settings\n",
|
||||
" )\n",
|
||||
" i = 0\n",
|
||||
" for result in results:\n",
|
||||
" print(f\"Result {i+1}: {str(result)}\")\n",
|
||||
" i += 1"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"id": "cdb8f740",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Multiple Azure OpenAI Chat Completions\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "66ba4767",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"az_oai_prompt_execution_settings = AzureChatPromptExecutionSettings(\n",
|
||||
" service_id=\"aoai_chat\",\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",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "b74a64a9",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"if selectedService == Service.AzureOpenAI:\n",
|
||||
" content = (\n",
|
||||
" \"Tomorrow 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",
|
||||
" )\n",
|
||||
" chat = ChatHistory()\n",
|
||||
" chat.add_user_message(content)\n",
|
||||
" results = await azure_chat_service.get_chat_message_contents(\n",
|
||||
" chat_history=chat, settings=az_oai_prompt_execution_settings\n",
|
||||
" )\n",
|
||||
" i = 0\n",
|
||||
" for result in results:\n",
|
||||
" print(f\"Result {i+1}: {str(result)}\")\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.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "26a37702",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"if selectedService == Service.OpenAI:\n",
|
||||
" import os\n",
|
||||
" import time\n",
|
||||
"\n",
|
||||
" from IPython.display import clear_output\n",
|
||||
"\n",
|
||||
" # Determine the clear command based on OS\n",
|
||||
" clear_command = \"cls\" if os.name == \"nt\" else \"clear\"\n",
|
||||
"\n",
|
||||
" chat = ChatHistory()\n",
|
||||
" chat.add_user_message(\"what is the purpose of a rubber duck?\")\n",
|
||||
"\n",
|
||||
" stream = oai_text_service.get_streaming_chat_message_contents(\n",
|
||||
" chat_history=chat, settings=oai_text_prompt_execution_settings\n",
|
||||
" )\n",
|
||||
" number_of_responses = oai_text_prompt_execution_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] += str(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.12.3"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
||||
@@ -149,7 +149,7 @@
|
||||
"source": [
|
||||
"if selectedService == Service.OpenAI:\n",
|
||||
" prompt = \"What is the purpose of a rubber duck?\"\n",
|
||||
" stream = oai_text_service.complete_stream(prompt=prompt, settings=oai_prompt_execution_settings)\n",
|
||||
" stream = oai_text_service.get_streaming_text_contents(prompt=prompt, settings=oai_prompt_execution_settings)\n",
|
||||
" async for message in stream:\n",
|
||||
" print(str(message[0]), end=\"\") # end = \"\" to avoid newlines"
|
||||
]
|
||||
@@ -172,7 +172,7 @@
|
||||
"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(prompt=prompt, settings=oai_prompt_execution_settings)\n",
|
||||
" stream = azure_text_service.get_streaming_text_contents(prompt=prompt, settings=oai_prompt_execution_settings)\n",
|
||||
" async for message in stream:\n",
|
||||
" print(str(message[0]), end=\"\")"
|
||||
]
|
||||
@@ -214,7 +214,9 @@
|
||||
"source": [
|
||||
"if selectedService == Service.HuggingFace:\n",
|
||||
" prompt = \"The purpose of a rubber duck is\"\n",
|
||||
" stream = hf_text_service.complete_stream(prompt=prompt, prompt_execution_settings=hf_prompt_execution_settings)\n",
|
||||
" stream = hf_text_service.get_streaming_text_contents(\n",
|
||||
" prompt=prompt, prompt_execution_settings=hf_prompt_execution_settings\n",
|
||||
" )\n",
|
||||
" async for text in stream:\n",
|
||||
" print(str(text[0]), end=\"\") # end = \"\" to avoid newlines"
|
||||
]
|
||||
@@ -265,7 +267,9 @@
|
||||
" content = \"You are an AI assistant that helps people find information.\"\n",
|
||||
" chat = ChatHistory()\n",
|
||||
" chat.add_system_message(content)\n",
|
||||
" stream = oai_chat_service.complete_chat_stream(chat_history=chat, settings=oai_chat_prompt_execution_settings)\n",
|
||||
" stream = oai_chat_service.get_streaming_chat_message_contents(\n",
|
||||
" chat_history=chat, settings=oai_chat_prompt_execution_settings\n",
|
||||
" )\n",
|
||||
" async for text in stream:\n",
|
||||
" print(str(text[0]), end=\"\") # end = \"\" to avoid newlines"
|
||||
]
|
||||
@@ -308,7 +312,9 @@
|
||||
" chat = ChatHistory()\n",
|
||||
" chat.add_system_message(content)\n",
|
||||
" chat.add_user_message(\"What is the purpose of a rubber duck?\")\n",
|
||||
" stream = azure_chat_service.complete_chat_stream(chat_history=chat, settings=az_oai_chat_prompt_execution_settings)\n",
|
||||
" stream = azure_chat_service.get_streaming_chat_message_contents(\n",
|
||||
" chat_history=chat, settings=az_oai_chat_prompt_execution_settings\n",
|
||||
" )\n",
|
||||
" async for text in stream:\n",
|
||||
" print(str(text[0]), end=\"\") # end = \"\" to avoid newlines"
|
||||
]
|
||||
|
||||
@@ -15,7 +15,7 @@ if TYPE_CHECKING:
|
||||
|
||||
class ChatCompletionClientBase(AIServiceClientBase, ABC):
|
||||
@abstractmethod
|
||||
async def complete_chat(
|
||||
async def get_chat_message_contents(
|
||||
self,
|
||||
chat_history: "ChatHistory",
|
||||
settings: "PromptExecutionSettings",
|
||||
@@ -36,7 +36,7 @@ class ChatCompletionClientBase(AIServiceClientBase, ABC):
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def complete_chat_stream(
|
||||
def get_streaming_chat_message_contents(
|
||||
self,
|
||||
chat_history: "ChatHistory",
|
||||
settings: "PromptExecutionSettings",
|
||||
|
||||
@@ -71,7 +71,7 @@ class GooglePalmChatCompletion(ChatCompletionClientBase, TextCompletionClientBas
|
||||
)
|
||||
self._message_history = message_history
|
||||
|
||||
async def complete_chat(
|
||||
async def get_chat_message_contents(
|
||||
self,
|
||||
chat_history: ChatHistory,
|
||||
settings: GooglePalmPromptExecutionSettings,
|
||||
@@ -122,7 +122,7 @@ class GooglePalmChatCompletion(ChatCompletionClientBase, TextCompletionClientBas
|
||||
content=candidate.get("content"),
|
||||
)
|
||||
|
||||
async def complete_chat_stream(
|
||||
async def get_streaming_chat_message_contents(
|
||||
self,
|
||||
messages: List[Tuple[str, str]],
|
||||
settings: GooglePalmPromptExecutionSettings,
|
||||
@@ -130,7 +130,7 @@ class GooglePalmChatCompletion(ChatCompletionClientBase, TextCompletionClientBas
|
||||
):
|
||||
raise NotImplementedError("Google Palm API does not currently support streaming")
|
||||
|
||||
async def complete(
|
||||
async def get_text_contents(
|
||||
self,
|
||||
prompt: str,
|
||||
settings: GooglePalmPromptExecutionSettings,
|
||||
@@ -169,7 +169,7 @@ class GooglePalmChatCompletion(ChatCompletionClientBase, TextCompletionClientBas
|
||||
text=candidate.get("content"),
|
||||
)
|
||||
|
||||
async def complete_stream(
|
||||
async def get_streaming_text_contents(
|
||||
self,
|
||||
prompt: str,
|
||||
settings: GooglePalmPromptExecutionSettings,
|
||||
|
||||
@@ -49,7 +49,9 @@ class GooglePalmTextCompletion(TextCompletionClientBase):
|
||||
|
||||
super().__init__(ai_model_id=ai_model_id, api_key=api_key)
|
||||
|
||||
async def complete(self, prompt: str, settings: GooglePalmTextPromptExecutionSettings) -> List[TextContent]:
|
||||
async def get_text_contents(
|
||||
self, prompt: str, settings: GooglePalmTextPromptExecutionSettings
|
||||
) -> List[TextContent]:
|
||||
"""
|
||||
This is the method that is called from the kernel to get a response from a text-optimized LLM.
|
||||
|
||||
@@ -93,7 +95,7 @@ class GooglePalmTextCompletion(TextCompletionClientBase):
|
||||
},
|
||||
)
|
||||
|
||||
async def complete_stream(
|
||||
async def get_streaming_text_contents(
|
||||
self,
|
||||
prompt: str,
|
||||
settings: GooglePalmTextPromptExecutionSettings,
|
||||
|
||||
@@ -73,7 +73,7 @@ class HuggingFaceTextCompletion(TextCompletionClientBase):
|
||||
generator=generator,
|
||||
)
|
||||
|
||||
async def complete(
|
||||
async def get_text_contents(
|
||||
self,
|
||||
prompt: str,
|
||||
settings: HuggingFacePromptExecutionSettings,
|
||||
@@ -103,7 +103,7 @@ class HuggingFaceTextCompletion(TextCompletionClientBase):
|
||||
text=candidate["summary_text" if self.task == "summarization" else "generated_text"],
|
||||
)
|
||||
|
||||
async def complete_stream(
|
||||
async def get_streaming_text_contents(
|
||||
self,
|
||||
prompt: str,
|
||||
settings: HuggingFacePromptExecutionSettings,
|
||||
|
||||
@@ -35,7 +35,7 @@ class OllamaChatCompletion(TextCompletionClientBase, ChatCompletionClientBase):
|
||||
url: HttpUrl = "http://localhost:11434/api/chat"
|
||||
session: Optional[aiohttp.ClientSession] = None
|
||||
|
||||
async def complete_chat(
|
||||
async def get_chat_message_contents(
|
||||
self,
|
||||
chat_history: ChatHistory,
|
||||
settings: OllamaChatPromptExecutionSettings,
|
||||
@@ -70,7 +70,7 @@ class OllamaChatCompletion(TextCompletionClientBase, ChatCompletionClientBase):
|
||||
)
|
||||
]
|
||||
|
||||
async def complete_chat_stream(
|
||||
async def get_streaming_chat_message_contents(
|
||||
self,
|
||||
chat_history: ChatHistory,
|
||||
settings: OllamaChatPromptExecutionSettings,
|
||||
@@ -112,7 +112,7 @@ class OllamaChatCompletion(TextCompletionClientBase, ChatCompletionClientBase):
|
||||
if body.get("done"):
|
||||
break
|
||||
|
||||
async def complete(
|
||||
async def get_text_contents(
|
||||
self,
|
||||
prompt: str,
|
||||
settings: OllamaChatPromptExecutionSettings,
|
||||
@@ -143,7 +143,7 @@ class OllamaChatCompletion(TextCompletionClientBase, ChatCompletionClientBase):
|
||||
)
|
||||
]
|
||||
|
||||
async def complete_stream(
|
||||
async def get_streaming_text_contents(
|
||||
self,
|
||||
prompt: str,
|
||||
settings: OllamaChatPromptExecutionSettings,
|
||||
|
||||
@@ -30,7 +30,7 @@ class OllamaTextCompletion(TextCompletionClientBase):
|
||||
url: HttpUrl = "http://localhost:11434/api/generate"
|
||||
session: Optional[aiohttp.ClientSession] = None
|
||||
|
||||
async def complete(
|
||||
async def get_text_contents(
|
||||
self,
|
||||
prompt: str,
|
||||
settings: OllamaTextPromptExecutionSettings,
|
||||
@@ -56,7 +56,7 @@ class OllamaTextCompletion(TextCompletionClientBase):
|
||||
text = inner_content["response"]
|
||||
return [TextContent(inner_content=inner_content, ai_model_id=self.ai_model_id, text=text)]
|
||||
|
||||
async def complete_stream(
|
||||
async def get_streaming_text_contents(
|
||||
self,
|
||||
prompt: str,
|
||||
settings: OllamaTextPromptExecutionSettings,
|
||||
|
||||
+2
-2
@@ -53,7 +53,7 @@ class OpenAIChatCompletionBase(OpenAIHandler, ChatCompletionClientBase):
|
||||
"""Create a request settings object."""
|
||||
return OpenAIChatPromptExecutionSettings
|
||||
|
||||
async def complete_chat(
|
||||
async def get_chat_message_contents(
|
||||
self,
|
||||
chat_history: ChatHistory,
|
||||
settings: OpenAIChatPromptExecutionSettings,
|
||||
@@ -100,7 +100,7 @@ class OpenAIChatCompletionBase(OpenAIHandler, ChatCompletionClientBase):
|
||||
)
|
||||
settings = self._prepare_settings(settings, chat_history, stream_request=False, kernel=kernel)
|
||||
|
||||
async def complete_chat_stream(
|
||||
async def get_streaming_chat_message_contents(
|
||||
self,
|
||||
chat_history: ChatHistory,
|
||||
settings: OpenAIChatPromptExecutionSettings,
|
||||
|
||||
+2
-2
@@ -31,7 +31,7 @@ class OpenAITextCompletionBase(OpenAIHandler, TextCompletionClientBase):
|
||||
"""Create a request settings object."""
|
||||
return OpenAITextPromptExecutionSettings
|
||||
|
||||
async def complete(
|
||||
async def get_text_contents(
|
||||
self,
|
||||
prompt: str,
|
||||
settings: "OpenAIPromptExecutionSettings",
|
||||
@@ -72,7 +72,7 @@ class OpenAITextCompletionBase(OpenAIHandler, TextCompletionClientBase):
|
||||
metadata=choice_metadata,
|
||||
)
|
||||
|
||||
async def complete_stream(
|
||||
async def get_streaming_text_contents(
|
||||
self,
|
||||
prompt: str,
|
||||
settings: "OpenAIPromptExecutionSettings",
|
||||
|
||||
@@ -15,7 +15,7 @@ class TextCompletionClientBase(AIServiceClientBase, ABC):
|
||||
"""Base class for text completion AI services."""
|
||||
|
||||
@abstractmethod
|
||||
async def complete(
|
||||
async def get_text_contents(
|
||||
self,
|
||||
prompt: str,
|
||||
settings: "PromptExecutionSettings",
|
||||
@@ -32,7 +32,7 @@ class TextCompletionClientBase(AIServiceClientBase, ABC):
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
def complete_stream(
|
||||
def get_streaming_text_contents(
|
||||
self,
|
||||
prompt: str,
|
||||
settings: "PromptExecutionSettings",
|
||||
|
||||
@@ -190,7 +190,7 @@ through prompt_template_config or in the prompt_template."
|
||||
kwargs["arguments"] = arguments
|
||||
|
||||
try:
|
||||
completions = await service.complete_chat(
|
||||
completions = await service.get_chat_message_contents(
|
||||
chat_history=chat_history,
|
||||
settings=execution_settings,
|
||||
**kwargs,
|
||||
@@ -211,7 +211,7 @@ through prompt_template_config or in the prompt_template."
|
||||
) -> FunctionResult:
|
||||
"""Handles the text service call."""
|
||||
try:
|
||||
completions = await service.complete(unescape(prompt), execution_settings)
|
||||
completions = await service.get_text_contents(unescape(prompt), execution_settings)
|
||||
return self._create_function_result(completions=completions, arguments=arguments, prompt=prompt)
|
||||
except Exception as exc:
|
||||
raise FunctionExecutionException(f"Error occurred while invoking function {self.name}: {exc}") from exc
|
||||
@@ -288,7 +288,7 @@ through prompt_template_config or in the prompt_template."
|
||||
|
||||
chat_history = ChatHistory.from_rendered_prompt(prompt)
|
||||
try:
|
||||
async for partial_content in service.complete_chat_stream(
|
||||
async for partial_content in service.get_streaming_chat_message_contents(
|
||||
chat_history=chat_history,
|
||||
settings=execution_settings,
|
||||
**kwargs,
|
||||
@@ -308,7 +308,9 @@ through prompt_template_config or in the prompt_template."
|
||||
) -> AsyncGenerator[FunctionResult | list[StreamingTextContent], Any]:
|
||||
"""Handles the text service call."""
|
||||
try:
|
||||
async for partial_content in service.complete_stream(prompt=prompt, settings=execution_settings):
|
||||
async for partial_content in service.get_streaming_text_contents(
|
||||
prompt=prompt, settings=execution_settings
|
||||
):
|
||||
yield partial_content
|
||||
return
|
||||
except Exception as e:
|
||||
|
||||
+1
-1
@@ -152,7 +152,7 @@ class FunctionCallingStepwisePlanner(KernelBaseModel):
|
||||
await asyncio.sleep(self.options.min_iteration_time_ms / 1000.0) # convert ms to sec
|
||||
# For each step, request another completion to select a function for that step
|
||||
chat_history_for_steps.add_user_message(STEPWISE_USER_MESSAGE)
|
||||
chat_result = await chat_completion.complete_chat(
|
||||
chat_result = await chat_completion.get_chat_message_contents(
|
||||
chat_history=chat_history_for_steps,
|
||||
settings=prompt_execution_settings,
|
||||
kernel=cloned_kernel,
|
||||
|
||||
@@ -62,7 +62,7 @@ async def test_google_palm_text_completion_complete_chat_call_with_parameters(go
|
||||
ai_model_id=ai_model_id,
|
||||
)
|
||||
settings = GooglePalmChatPromptExecutionSettings()
|
||||
response = await gp_chat_completion.complete_chat(chats, settings)
|
||||
response = await gp_chat_completion.get_chat_message_contents(chats, settings)
|
||||
|
||||
assert isinstance(response[0].content, str) and len(response) > 0
|
||||
print(mock_gp.chat)
|
||||
|
||||
@@ -6,12 +6,8 @@ from google.generativeai.types import Completion
|
||||
from google.generativeai.types.text_types import TextCompletion
|
||||
from pydantic import ValidationError
|
||||
|
||||
from semantic_kernel.connectors.ai.google_palm import (
|
||||
GooglePalmTextPromptExecutionSettings,
|
||||
)
|
||||
from semantic_kernel.connectors.ai.google_palm.services.gp_text_completion import (
|
||||
GooglePalmTextCompletion,
|
||||
)
|
||||
from semantic_kernel.connectors.ai.google_palm import GooglePalmTextPromptExecutionSettings
|
||||
from semantic_kernel.connectors.ai.google_palm.services.gp_text_completion import GooglePalmTextCompletion
|
||||
|
||||
|
||||
def test_google_palm_text_completion_init(google_palm_unit_test_env) -> None:
|
||||
@@ -55,7 +51,7 @@ async def test_google_palm_text_completion_complete_call_with_parameters(google_
|
||||
ai_model_id=ai_model_id,
|
||||
)
|
||||
settings = GooglePalmTextPromptExecutionSettings()
|
||||
response = await gp_text_completion.complete(prompt, settings)
|
||||
response = await gp_text_completion.get_text_contents(prompt, settings)
|
||||
assert isinstance(response[0].text, str) and len(response) > 0
|
||||
|
||||
mock_gp.generate_text.assert_called_once_with(
|
||||
|
||||
@@ -2,12 +2,8 @@ from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
|
||||
from semantic_kernel.connectors.ai.ollama.ollama_prompt_execution_settings import (
|
||||
OllamaChatPromptExecutionSettings,
|
||||
)
|
||||
from semantic_kernel.connectors.ai.ollama.services.ollama_chat_completion import (
|
||||
OllamaChatCompletion,
|
||||
)
|
||||
from semantic_kernel.connectors.ai.ollama.ollama_prompt_execution_settings import OllamaChatPromptExecutionSettings
|
||||
from semantic_kernel.connectors.ai.ollama.services.ollama_chat_completion import OllamaChatCompletion
|
||||
from semantic_kernel.contents.chat_history import ChatHistory
|
||||
from tests.unit.connectors.ollama.utils import MockResponse
|
||||
|
||||
@@ -25,7 +21,7 @@ async def test_complete_chat(mock_post):
|
||||
ollama = OllamaChatCompletion(ai_model_id="test_model")
|
||||
chat_history = ChatHistory()
|
||||
chat_history.add_user_message("test_prompt")
|
||||
response = await ollama.complete_chat(
|
||||
response = await ollama.get_chat_message_contents(
|
||||
chat_history,
|
||||
OllamaChatPromptExecutionSettings(service_id="test_model", ai_model_id="test_model", options={"test": "test"}),
|
||||
)
|
||||
@@ -46,7 +42,7 @@ async def test_complete_chat(mock_post):
|
||||
async def test_complete(mock_post):
|
||||
mock_post.return_value = MockResponse(response={"message": {"content": "test_response"}})
|
||||
ollama = OllamaChatCompletion(ai_model_id="test_model")
|
||||
response = await ollama.complete(
|
||||
response = await ollama.get_text_contents(
|
||||
"test_prompt",
|
||||
OllamaChatPromptExecutionSettings(service_id="test_model", ai_model_id="test_model", options={"test": "test"}),
|
||||
)
|
||||
@@ -60,7 +56,7 @@ async def test_complete_chat_stream(mock_post):
|
||||
ollama = OllamaChatCompletion(ai_model_id="test_model")
|
||||
chat_history = ChatHistory()
|
||||
chat_history.add_user_message("test_prompt")
|
||||
response = ollama.complete_chat_stream(
|
||||
response = ollama.get_streaming_chat_message_contents(
|
||||
chat_history,
|
||||
OllamaChatPromptExecutionSettings(ai_model_id="test_model", options={"test": "test"}),
|
||||
)
|
||||
@@ -83,7 +79,7 @@ async def test_complete_chat_stream(mock_post):
|
||||
async def test_complete_stream(mock_post):
|
||||
mock_post.return_value = MockResponse(response={"message": {"content": "test_response"}})
|
||||
ollama = OllamaChatCompletion(ai_model_id="test_model")
|
||||
response = ollama.complete_stream(
|
||||
response = ollama.get_streaming_text_contents(
|
||||
"test_prompt",
|
||||
OllamaChatPromptExecutionSettings(ai_model_id="test_model", options={"test": "test"}),
|
||||
)
|
||||
|
||||
@@ -2,12 +2,8 @@ from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
|
||||
from semantic_kernel.connectors.ai.ollama.ollama_prompt_execution_settings import (
|
||||
OllamaTextPromptExecutionSettings,
|
||||
)
|
||||
from semantic_kernel.connectors.ai.ollama.services.ollama_text_completion import (
|
||||
OllamaTextCompletion,
|
||||
)
|
||||
from semantic_kernel.connectors.ai.ollama.ollama_prompt_execution_settings import OllamaTextPromptExecutionSettings
|
||||
from semantic_kernel.connectors.ai.ollama.services.ollama_text_completion import OllamaTextCompletion
|
||||
from tests.unit.connectors.ollama.utils import MockResponse
|
||||
|
||||
|
||||
@@ -22,7 +18,7 @@ def test_settings():
|
||||
async def test_complete(mock_post):
|
||||
mock_post.return_value = MockResponse(response={"response": "test_response"})
|
||||
ollama = OllamaTextCompletion(ai_model_id="test_model")
|
||||
response = await ollama.complete(
|
||||
response = await ollama.get_text_contents(
|
||||
"test prompt",
|
||||
OllamaTextPromptExecutionSettings(options={"test": "test"}),
|
||||
)
|
||||
@@ -34,7 +30,7 @@ async def test_complete(mock_post):
|
||||
async def test_complete_stream(mock_post):
|
||||
mock_post.return_value = MockResponse(response={"response": "test_response"})
|
||||
ollama = OllamaTextCompletion(ai_model_id="test_model")
|
||||
response = ollama.complete_stream(
|
||||
response = ollama.get_streaming_text_contents(
|
||||
"test_prompt",
|
||||
OllamaTextPromptExecutionSettings(options={"test": "test"}),
|
||||
)
|
||||
|
||||
@@ -89,7 +89,7 @@ async def test_azure_chat_completion_call_with_parameters(
|
||||
complete_prompt_execution_settings = AzureChatPromptExecutionSettings(service_id="test_service_id")
|
||||
|
||||
azure_chat_completion = AzureChatCompletion()
|
||||
await azure_chat_completion.complete_chat(
|
||||
await azure_chat_completion.get_chat_message_contents(
|
||||
chat_history=chat_history, settings=complete_prompt_execution_settings, kernel=kernel
|
||||
)
|
||||
mock_create.assert_awaited_once_with(
|
||||
@@ -120,7 +120,7 @@ async def test_azure_chat_completion_call_with_parameters_and_Logit_Bias_Defined
|
||||
|
||||
azure_chat_completion = AzureChatCompletion()
|
||||
|
||||
await azure_chat_completion.complete_chat(
|
||||
await azure_chat_completion.get_chat_message_contents(
|
||||
chat_history=chat_history, settings=complete_prompt_execution_settings, kernel=kernel
|
||||
)
|
||||
|
||||
@@ -153,7 +153,7 @@ async def test_azure_chat_completion_call_with_parameters_and_Stop_Defined(
|
||||
|
||||
azure_chat_completion = AzureChatCompletion()
|
||||
|
||||
await azure_chat_completion.complete(prompt=prompt, settings=complete_prompt_execution_settings)
|
||||
await azure_chat_completion.get_text_contents(prompt=prompt, settings=complete_prompt_execution_settings)
|
||||
|
||||
mock_create.assert_awaited_once_with(
|
||||
model=azure_openai_unit_test_env["AZURE_OPENAI_CHAT_DEPLOYMENT_NAME"],
|
||||
@@ -226,7 +226,7 @@ async def test_azure_chat_completion_with_data_call_with_parameters(
|
||||
|
||||
azure_chat_completion = AzureChatCompletion()
|
||||
|
||||
await azure_chat_completion.complete_chat(
|
||||
await azure_chat_completion.get_chat_message_contents(
|
||||
chat_history=messages_in, settings=complete_prompt_execution_settings, kernel=kernel
|
||||
)
|
||||
|
||||
@@ -271,7 +271,7 @@ async def test_azure_chat_completion_call_with_data_parameters_and_function_call
|
||||
extra_body=extra,
|
||||
)
|
||||
|
||||
await azure_chat_completion.complete_chat(
|
||||
await azure_chat_completion.get_chat_message_contents(
|
||||
chat_history=chat_history,
|
||||
settings=complete_prompt_execution_settings,
|
||||
kernel=kernel,
|
||||
@@ -320,7 +320,9 @@ async def test_azure_chat_completion_call_with_data_with_parameters_and_Stop_Def
|
||||
|
||||
azure_chat_completion = AzureChatCompletion()
|
||||
|
||||
await azure_chat_completion.complete_chat(chat_history, complete_prompt_execution_settings, kernel=kernel)
|
||||
await azure_chat_completion.get_chat_message_contents(
|
||||
chat_history, complete_prompt_execution_settings, kernel=kernel
|
||||
)
|
||||
|
||||
expected_data_settings = extra.model_dump(exclude_none=True, by_alias=True)
|
||||
|
||||
@@ -387,7 +389,9 @@ async def test_azure_chat_completion_content_filtering_raises_correct_exception(
|
||||
azure_chat_completion = AzureChatCompletion()
|
||||
|
||||
with pytest.raises(ContentFilterAIException, match="service encountered a content error") as exc_info:
|
||||
await azure_chat_completion.complete_chat(chat_history, complete_prompt_execution_settings, kernel=kernel)
|
||||
await azure_chat_completion.get_chat_message_contents(
|
||||
chat_history, complete_prompt_execution_settings, kernel=kernel
|
||||
)
|
||||
|
||||
content_filter_exc = exc_info.value
|
||||
assert content_filter_exc.param == "prompt"
|
||||
@@ -428,7 +432,9 @@ async def test_azure_chat_completion_content_filtering_without_response_code_rai
|
||||
azure_chat_completion = AzureChatCompletion()
|
||||
|
||||
with pytest.raises(ContentFilterAIException, match="service encountered a content error"):
|
||||
await azure_chat_completion.complete_chat(chat_history, complete_prompt_execution_settings, kernel=kernel)
|
||||
await azure_chat_completion.get_chat_message_contents(
|
||||
chat_history, complete_prompt_execution_settings, kernel=kernel
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@@ -448,7 +454,9 @@ async def test_azure_chat_completion_bad_request_non_content_filter(
|
||||
azure_chat_completion = AzureChatCompletion()
|
||||
|
||||
with pytest.raises(ServiceResponseException, match="service failed to complete the prompt"):
|
||||
await azure_chat_completion.complete_chat(chat_history, complete_prompt_execution_settings, kernel=kernel)
|
||||
await azure_chat_completion.get_chat_message_contents(
|
||||
chat_history, complete_prompt_execution_settings, kernel=kernel
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@@ -473,4 +481,4 @@ async def test_azure_chat_completion_no_kernel_provided_throws_error(
|
||||
ServiceInvalidExecutionSettingsError,
|
||||
match="The kernel argument and arguments are required for auto invoking OpenAI tool calls.",
|
||||
):
|
||||
await azure_chat_completion.complete_chat(chat_history, complete_prompt_execution_settings)
|
||||
await azure_chat_completion.get_chat_message_contents(chat_history, complete_prompt_execution_settings)
|
||||
|
||||
@@ -74,7 +74,7 @@ async def test_azure_text_completion_call_with_parameters(mock_create, azure_ope
|
||||
complete_prompt_execution_settings = OpenAITextPromptExecutionSettings()
|
||||
azure_text_completion = AzureTextCompletion()
|
||||
|
||||
await azure_text_completion.complete(prompt, complete_prompt_execution_settings)
|
||||
await azure_text_completion.get_text_contents(prompt, complete_prompt_execution_settings)
|
||||
|
||||
mock_create.assert_awaited_once_with(
|
||||
model=azure_openai_unit_test_env["AZURE_OPENAI_TEXT_DEPLOYMENT_NAME"],
|
||||
@@ -105,7 +105,7 @@ async def test_azure_text_completion_call_with_parameters_logit_bias_not_none(
|
||||
|
||||
azure_text_completion = AzureTextCompletion()
|
||||
|
||||
await azure_text_completion.complete(prompt, complete_prompt_execution_settings)
|
||||
await azure_text_completion.get_text_contents(prompt, complete_prompt_execution_settings)
|
||||
|
||||
mock_create.assert_awaited_once_with(
|
||||
model=azure_openai_unit_test_env["AZURE_OPENAI_TEXT_DEPLOYMENT_NAME"],
|
||||
|
||||
@@ -6,11 +6,7 @@ import pytest
|
||||
from openai import AsyncOpenAI
|
||||
|
||||
from semantic_kernel.connectors.ai.open_ai.services.open_ai_chat_completion import OpenAIChatCompletionBase
|
||||
from semantic_kernel.contents import (
|
||||
ChatMessageContent,
|
||||
StreamingChatMessageContent,
|
||||
TextContent,
|
||||
)
|
||||
from semantic_kernel.contents import ChatMessageContent, StreamingChatMessageContent, TextContent
|
||||
from semantic_kernel.contents.chat_history import ChatHistory
|
||||
from semantic_kernel.contents.function_call_content import FunctionCallContent
|
||||
from semantic_kernel.exceptions import FunctionCallInvalidArgumentsException
|
||||
@@ -44,7 +40,7 @@ async def test_complete_chat_stream(kernel: Kernel):
|
||||
ai_model_id="test_model_id", service_id="test", client=MagicMock(spec=AsyncOpenAI)
|
||||
)
|
||||
|
||||
async for content in chat_completion_base.complete_chat_stream(
|
||||
async for content in chat_completion_base.get_streaming_chat_message_contents(
|
||||
chat_history, settings, kernel=kernel, arguments=arguments
|
||||
):
|
||||
assert content is not None
|
||||
@@ -77,7 +73,9 @@ async def test_complete_chat(tool_call, kernel: Kernel):
|
||||
ai_model_id="test_model_id", service_id="test", client=MagicMock(spec=AsyncOpenAI)
|
||||
)
|
||||
|
||||
result = await chat_completion_base.complete_chat(chat_history, settings, kernel=kernel, arguments=arguments)
|
||||
result = await chat_completion_base.get_chat_message_contents(
|
||||
chat_history, settings, kernel=kernel, arguments=arguments
|
||||
)
|
||||
|
||||
if tool_call:
|
||||
assert result is None
|
||||
|
||||
@@ -153,14 +153,14 @@ async def test_invoke_chat_stream(openai_unit_test_env):
|
||||
|
||||
# This part remains unchanged - for synchronous mocking example
|
||||
with patch(
|
||||
"semantic_kernel.connectors.ai.open_ai.services.open_ai_chat_completion.OpenAIChatCompletion.complete_chat"
|
||||
"semantic_kernel.connectors.ai.open_ai.services.open_ai_chat_completion.OpenAIChatCompletion.get_chat_message_contents"
|
||||
) as mock:
|
||||
mock.return_value = [ChatMessageContent(role="assistant", content="test", metadata={})]
|
||||
result = await function.invoke(kernel=kernel)
|
||||
assert str(result) == "test"
|
||||
|
||||
with patch(
|
||||
"semantic_kernel.connectors.ai.open_ai.services.open_ai_chat_completion.OpenAIChatCompletion.complete_chat_stream"
|
||||
"semantic_kernel.connectors.ai.open_ai.services.open_ai_chat_completion.OpenAIChatCompletion.get_streaming_chat_message_contents"
|
||||
) as mock:
|
||||
mock.__iter__.return_value = [
|
||||
StreamingChatMessageContent(choice_index=0, role="assistant", content="test", metadata={})
|
||||
@@ -180,7 +180,7 @@ async def test_invoke_exception(openai_unit_test_env):
|
||||
prompt_execution_settings=PromptExecutionSettings(service_id="test"),
|
||||
)
|
||||
with patch(
|
||||
"semantic_kernel.connectors.ai.open_ai.services.open_ai_chat_completion.OpenAIChatCompletion.complete_chat",
|
||||
"semantic_kernel.connectors.ai.open_ai.services.open_ai_chat_completion.OpenAIChatCompletion.get_chat_message_contents",
|
||||
side_effect=Exception,
|
||||
) as mock:
|
||||
mock.return_value = [ChatMessageContent(role="assistant", content="test", metadata={})]
|
||||
@@ -188,7 +188,7 @@ async def test_invoke_exception(openai_unit_test_env):
|
||||
assert isinstance(result.metadata[METADATA_EXCEPTION_KEY], Exception)
|
||||
|
||||
with patch(
|
||||
"semantic_kernel.connectors.ai.open_ai.services.open_ai_chat_completion.OpenAIChatCompletion.complete_chat_stream",
|
||||
"semantic_kernel.connectors.ai.open_ai.services.open_ai_chat_completion.OpenAIChatCompletion.get_streaming_chat_message_contents",
|
||||
side_effect=Exception,
|
||||
) as mock:
|
||||
mock.__iter__.return_value = [
|
||||
@@ -209,14 +209,14 @@ async def test_invoke_text(openai_unit_test_env):
|
||||
prompt_execution_settings=PromptExecutionSettings(service_id="test"),
|
||||
)
|
||||
with patch(
|
||||
"semantic_kernel.connectors.ai.open_ai.services.open_ai_text_completion.OpenAITextCompletion.complete",
|
||||
"semantic_kernel.connectors.ai.open_ai.services.open_ai_text_completion.OpenAITextCompletion.get_text_contents",
|
||||
) as mock:
|
||||
mock.return_value = [TextContent(text="test", metadata={})]
|
||||
result = await function.invoke(kernel=kernel)
|
||||
assert str(result) == "test"
|
||||
|
||||
with patch(
|
||||
"semantic_kernel.connectors.ai.open_ai.services.open_ai_text_completion.OpenAITextCompletion.complete_stream",
|
||||
"semantic_kernel.connectors.ai.open_ai.services.open_ai_text_completion.OpenAITextCompletion.get_streaming_text_contents",
|
||||
) as mock:
|
||||
mock.__iter__.return_value = [TextContent(text="test", metadata={})]
|
||||
async for result in function.invoke_stream(kernel=kernel):
|
||||
@@ -234,7 +234,7 @@ async def test_invoke_exception_text(openai_unit_test_env):
|
||||
prompt_execution_settings=PromptExecutionSettings(service_id="test"),
|
||||
)
|
||||
with patch(
|
||||
"semantic_kernel.connectors.ai.open_ai.services.open_ai_text_completion.OpenAITextCompletion.complete",
|
||||
"semantic_kernel.connectors.ai.open_ai.services.open_ai_text_completion.OpenAITextCompletion.get_text_contents",
|
||||
side_effect=Exception,
|
||||
) as mock:
|
||||
mock.return_value = [TextContent(text="test", metadata={})]
|
||||
@@ -242,7 +242,7 @@ async def test_invoke_exception_text(openai_unit_test_env):
|
||||
assert isinstance(result.metadata[METADATA_EXCEPTION_KEY], Exception)
|
||||
|
||||
with patch(
|
||||
"semantic_kernel.connectors.ai.open_ai.services.open_ai_text_completion.OpenAITextCompletion.complete_stream",
|
||||
"semantic_kernel.connectors.ai.open_ai.services.open_ai_text_completion.OpenAITextCompletion.get_streaming_text_contents",
|
||||
side_effect=Exception,
|
||||
) as mock:
|
||||
mock.__iter__.return_value = []
|
||||
@@ -264,7 +264,7 @@ async def test_invoke_defaults(openai_unit_test_env):
|
||||
prompt_execution_settings=PromptExecutionSettings(service_id="test"),
|
||||
)
|
||||
with patch(
|
||||
"semantic_kernel.connectors.ai.open_ai.services.open_ai_chat_completion.OpenAIChatCompletion.complete_chat"
|
||||
"semantic_kernel.connectors.ai.open_ai.services.open_ai_chat_completion.OpenAIChatCompletion.get_chat_message_contents"
|
||||
) as mock:
|
||||
mock.return_value = [ChatMessageContent(role="assistant", content="test", metadata={})]
|
||||
result = await function.invoke(kernel=kernel)
|
||||
@@ -307,7 +307,7 @@ async def test_create_with_multiple_settings_one_service_registered(openai_unit_
|
||||
),
|
||||
)
|
||||
with patch(
|
||||
"semantic_kernel.connectors.ai.open_ai.services.open_ai_chat_completion.OpenAIChatCompletion.complete_chat"
|
||||
"semantic_kernel.connectors.ai.open_ai.services.open_ai_chat_completion.OpenAIChatCompletion.get_chat_message_contents"
|
||||
) as mock:
|
||||
mock.return_value = [ChatMessageContent(role="assistant", content="test", metadata={})]
|
||||
result = await function.invoke(kernel=kernel)
|
||||
|
||||
Reference in New Issue
Block a user