-
Notifications
You must be signed in to change notification settings - Fork 4.8k
docs: add PZERO OpenAI-compatible endpoint sample and documentation #14350
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MOHAMMED WASIM KHAN (wasim-builds)
wants to merge
1
commit into
microsoft:main
Choose a base branch
from
wasim-builds:docs/pzero-openai-compatible-sample-14347
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
dotnet/samples/Concepts/ChatCompletion/PZero_ChatCompletion.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| using Microsoft.SemanticKernel; | ||
| using Microsoft.SemanticKernel.ChatCompletion; | ||
| using Microsoft.SemanticKernel.Connectors.OpenAI; | ||
|
|
||
| namespace ChatCompletion; | ||
|
|
||
| /// <summary> | ||
| /// This example shows how to use the OpenAI connector with PZERO's OpenAI-compatible endpoint. | ||
| /// <list type="number"> | ||
| /// <item>Get a key from https://pzero.studio/agents</item> | ||
| /// <item>Set the environment variable PZERO_API_KEY with your key</item> | ||
| /// <item>Configure the endpoint to https://api.pzero.studio/v1</item> | ||
| /// <item>Run the example</item> | ||
| /// </list> | ||
| /// </summary> | ||
| public class PZero_ChatCompletion(ITestOutputHelper output) : BaseTest(output) | ||
| { | ||
| /// <summary> | ||
| /// This example shows how to configure PZERO OpenAI-compatible endpoint with Kernel InvokeAsync. | ||
| /// </summary> | ||
| [Fact] | ||
| public async Task UsingKernelWithPZero() | ||
| { | ||
| Console.WriteLine($"======== PZERO - Chat Completion - {nameof(UsingKernelWithPZero)} ========"); | ||
|
|
||
| var apiKey = Environment.GetEnvironmentVariable("PZERO_API_KEY"); | ||
| if (string.IsNullOrEmpty(apiKey)) | ||
| { | ||
| Console.WriteLine("PZERO_API_KEY environment variable is not set. Skipping execution."); | ||
| return; | ||
| } | ||
|
|
||
| var modelId = "deepseek-v4-flash"; | ||
| var endpoint = new Uri("https://api.pzero.studio/v1"); | ||
|
|
||
| var kernel = Kernel.CreateBuilder() | ||
| .AddOpenAIChatCompletion( | ||
| modelId: modelId, | ||
| endpoint: endpoint, | ||
| apiKey: apiKey) | ||
| .Build(); | ||
|
|
||
| var prompt = @"Rewrite the text between triple backticks into a business email. Use a professional tone, be clear and concise. | ||
| Sign the email as AI Assistant. | ||
|
|
||
| Text: ```{{$input}}```"; | ||
|
|
||
| var mailFunction = kernel.CreateFunctionFromPrompt(prompt, new OpenAIPromptExecutionSettings | ||
| { | ||
| TopP = 0.5, | ||
| MaxTokens = 1000, | ||
| }); | ||
|
|
||
| var response = await kernel.InvokeAsync(mailFunction, new() { ["input"] = "Tell David that I will complete the report by Friday." }); | ||
| Console.WriteLine(response); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Sample showing how to use <see cref="IChatCompletionService"/> directly with a <see cref="ChatHistory"/> against PZERO. | ||
| /// </summary> | ||
| [Fact] | ||
| public async Task UsingServiceNonStreamingWithPZero() | ||
| { | ||
| Console.WriteLine($"======== PZERO - Chat Completion - {nameof(UsingServiceNonStreamingWithPZero)} ========"); | ||
|
|
||
| var apiKey = Environment.GetEnvironmentVariable("PZERO_API_KEY"); | ||
| if (string.IsNullOrEmpty(apiKey)) | ||
| { | ||
| Console.WriteLine("PZERO_API_KEY environment variable is not set. Skipping execution."); | ||
| return; | ||
| } | ||
|
|
||
| var modelId = "deepseek-v4-flash"; | ||
| var endpoint = new Uri("https://api.pzero.studio/v1"); | ||
|
|
||
| OpenAIChatCompletionService chatService = new(modelId: modelId, endpoint: endpoint, apiKey: apiKey); | ||
|
|
||
| Console.WriteLine("Chat content:"); | ||
| Console.WriteLine("------------------------"); | ||
|
|
||
| var chatHistory = new ChatHistory("You are a helpful assistant."); | ||
|
|
||
| chatHistory.AddUserMessage("Hello! Can you summarize the purpose of Semantic Kernel in one sentence?"); | ||
| this.OutputLastMessage(chatHistory); | ||
|
|
||
| var reply = await chatService.GetChatMessageContentAsync(chatHistory); | ||
| chatHistory.Add(reply); | ||
| this.OutputLastMessage(chatHistory); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
python/samples/concepts/chat_completion/pzero_chat_completion.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| # Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| import asyncio | ||
| import os | ||
|
|
||
| from openai import AsyncOpenAI | ||
|
|
||
| from semantic_kernel.connectors.ai.open_ai import OpenAIChatCompletion | ||
| from semantic_kernel.contents.chat_history import ChatHistory | ||
| from semantic_kernel.functions.kernel_arguments import KernelArguments | ||
| from semantic_kernel.kernel import Kernel | ||
|
|
||
| # This concept sample shows how to use the OpenAI connector with PZERO's | ||
| # OpenAI-compatible endpoint: https://api.pzero.studio/v1 | ||
| # Get an API key from https://pzero.studio/agents and set PZERO_API_KEY. | ||
|
|
||
| system_message = """ | ||
| You are a helpful and concise AI assistant. | ||
| """ | ||
|
|
||
| kernel = Kernel() | ||
|
|
||
| service_id = "pzero-deepseek" | ||
|
|
||
| api_key = os.environ.get("PZERO_API_KEY", "your-pzero-api-key") | ||
| endpoint = "https://api.pzero.studio/v1" | ||
| model_id = "deepseek-v4-flash" | ||
|
|
||
| open_ai_client: AsyncOpenAI = AsyncOpenAI( | ||
| api_key=api_key, | ||
| base_url=endpoint, | ||
| ) | ||
| kernel.add_service(OpenAIChatCompletion(service_id=service_id, ai_model_id=model_id, async_client=open_ai_client)) | ||
|
|
||
| settings = kernel.get_prompt_execution_settings_from_service_id(service_id) | ||
| settings.max_tokens = 1000 | ||
| settings.temperature = 0.7 | ||
|
|
||
| chat_function = kernel.add_function( | ||
| plugin_name="ChatBot", | ||
| function_name="Chat", | ||
| prompt="{{$chat_history}}{{$user_input}}", | ||
| template_format="semantic-kernel", | ||
| prompt_execution_settings=settings, | ||
| ) | ||
|
|
||
|
|
||
| async def main() -> None: | ||
| chat_history = ChatHistory(system_message=system_message) | ||
| user_message = "What is Semantic Kernel in one sentence?" | ||
| chat_history.add_user_message(user_message) | ||
|
|
||
| answer = await kernel.invoke(chat_function, KernelArguments(user_input=user_message, chat_history=chat_history)) | ||
| print(f"User:> {user_message}") | ||
| print(f"Assistant:> {answer}") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| asyncio.run(main()) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This sample cannot run as written. The function is registered with a
{{$chat_history}}template but without enabling dangerous content, andkernel.invokeis later called with aChatHistoryobject bound tochat_history. During prompt rendering, complex (non-string) argument values are rejected unless dangerous content is allowed, so the invoke raisesNotImplementedError: Argument 'chat_history' has a value that doesn't support automatic encoding...before any request reaches the endpoint.py_compiledoes not exercise this path, so it passes while the sample fails on every run. Register the template the same way the workingsimple_chatbot_kernel_function.pysample does — via aPromptTemplateConfig(template="{{$chat_history}}{{$user_input}}", template_format="semantic-kernel", allow_dangerously_set_content=True)passed asprompt_template_config— so the trusted chat history renders and the sample can complete a call.