-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
Python: Feat/Add DEVELOPER role for openai o1 #10033
Open
ymuichiro
wants to merge
13
commits into
microsoft:main
Choose a base branch
from
ymuichiro:feat/python-add-developer-role-for-openai-o1
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.
+402
−47
Open
Changes from 12 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
076c792
Python: In Azure OpenAI, stream_options is now enabled, so the overri…
ymuichiro 40587be
Python: Removed unnecessary imports from azure_chat_completion.py
ymuichiro 1466cd2
Merge branch 'main' into main
TaoChenOSU e7ae76e
Python: Align test arguments with enforced stream_options in OpenAICh…
ymuichiro 292e577
Merge branch 'main' into main
TaoChenOSU 7bfda00
Merge branch 'microsoft:main' into main
ymuichiro afb791d
python: add developer role and o1 properties (max_completion_tokens, …
ymuichiro f41123a
Merge branch 'main' into feat/python-add-developer-role-for-openai-o1
moonbox3 3872087
python: Add developer message formatting function and update message …
ymuichiro 86e23ba
Python: Add samples for reasoning models and update OpenAI prompt exe…
ymuichiro 792af89
update openai-python sdk version
ymuichiro 512895d
Remove redundant 'stream' parameter from simple_reasoning.py
ymuichiro fd3e0dc
Added Reasoning to samples/concepts/README.md category
ymuichiro 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 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,128 @@ | ||
# Copyright (c) Microsoft. All rights reserved. | ||
|
||
import asyncio | ||
|
||
from samples.concepts.setup.chat_completion_services import ( | ||
Services, | ||
get_chat_completion_service_and_request_settings, | ||
) | ||
from semantic_kernel.connectors.ai.open_ai.prompt_execution_settings.open_ai_prompt_execution_settings import ( | ||
OpenAIChatPromptExecutionSettings, | ||
) | ||
from semantic_kernel.contents import ChatHistory | ||
|
||
""" | ||
# Reasoning Models Sample | ||
|
||
This sample demonstrates an example of how to use reasoning models such as OpenAI’s o1 and o1-mini for inference. | ||
Reasoning models currently have certain limitations, which are outlined below. | ||
|
||
1. Requires API version `2024-09-01-preview` or later. | ||
- `reasoning_effort` and `developer_message` are only supported in API version `2024-12-01-preview` or later. | ||
- o1-mini is not supported property `developer_message` `reasoning_effort` now. | ||
2. Developer message must be used instead of system message | ||
3. Parallel tool invocation is currently not supported | ||
4. Token limit settings need to consider both reasoning and completion tokens | ||
|
||
# Unsupported Properties ⛔ | ||
|
||
The following parameters are currently not supported: | ||
- temperature | ||
- top_p | ||
- presence_penalty | ||
- frequency_penalty | ||
- logprobs | ||
- top_logprobs | ||
- logit_bias | ||
- max_tokens | ||
- stream | ||
- tool_choice | ||
|
||
# .env examples | ||
|
||
OpenAI: semantic_kernel/connectors/ai/open_ai/settings/open_ai_settings.py | ||
|
||
```.env | ||
OPENAI_API_KEY=******************* | ||
OPENAI_CHAT_MODEL_ID=o1-2024-12-17 | ||
``` | ||
|
||
Azure OpenAI: semantic_kernel/connectors/ai/open_ai/settings/azure_open_ai_settings.py | ||
|
||
```.env | ||
AZURE_OPENAI_API_KEY=******************* | ||
AZURE_OPENAI_ENDPOINT=https://*********.openai.azure.com | ||
AZURE_OPENAI_CHAT_DEPLOYMENT_NAME=o1-2024-12-17 | ||
AZURE_OPENAI_API_VERSION="2024-12-01-preview" | ||
``` | ||
|
||
Note: Unsupported features may be added in future updates. | ||
""" | ||
|
||
chat_completion_service, request_settings = get_chat_completion_service_and_request_settings(Services.OPENAI) | ||
|
||
# This is the system message that gives the chatbot its personality. | ||
developer_message = """ | ||
As an assistant supporting the user, | ||
you recognize all user input | ||
as questions or consultations and answer them. | ||
""" | ||
|
||
# Create a ChatHistory object | ||
chat_history = ChatHistory() | ||
|
||
|
||
async def chat() -> bool: | ||
try: | ||
user_input = input("User:> ") | ||
except KeyboardInterrupt: | ||
print("\n\nExiting chat...") | ||
return False | ||
except EOFError: | ||
print("\n\nExiting chat...") | ||
return False | ||
|
||
if user_input == "exit": | ||
print("\n\nExiting chat...") | ||
return False | ||
|
||
# The developer message was newly introduced for reasoning models such as OpenAI’s o1 and o1-mini. | ||
# `system message` cannot be used with reasoning models. | ||
chat_history.add_developer_message(developer_message) | ||
chat_history.add_user_message(user_input) | ||
|
||
if not isinstance(request_settings, OpenAIChatPromptExecutionSettings): | ||
raise ValueError("The OpenAI prompt execution settings are not supported for this sample.") | ||
|
||
# Set the reasoning effort to "medium" and the maximum completion tokens to 5000. | ||
request_settings.max_completion_tokens = 5000 | ||
request_settings.reasoning_effort = "medium" | ||
|
||
# Get the chat message content from the chat completion service. | ||
response = await chat_completion_service.get_chat_message_content( | ||
chat_history=chat_history, | ||
settings=request_settings, | ||
) | ||
if response: | ||
print(f"Mosscap:> {response}") | ||
|
||
# Add the chat message to the chat history to keep track of the conversation. | ||
chat_history.add_message(response) | ||
|
||
return True | ||
|
||
|
||
async def main() -> None: | ||
# Start the chat loop. The chat loop will continue until the user types "exit". | ||
chatting = True | ||
while chatting: | ||
chatting = await chat() | ||
|
||
# Sample output: | ||
# User:> Why is the sky blue in one sentence? | ||
# Mosscap:> The sky appears blue because air molecules in the atmosphere scatter shorter-wavelength (blue) | ||
# light more efficiently than longer-wavelength (red) light. | ||
|
||
|
||
if __name__ == "__main__": | ||
asyncio.run(main()) |
193 changes: 193 additions & 0 deletions
193
python/samples/concepts/reasoning/simple_reasoning_function_calling.py
This file contains 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,193 @@ | ||
# Copyright (c) Microsoft. All rights reserved. | ||
|
||
import asyncio | ||
|
||
from samples.concepts.setup.chat_completion_services import ( | ||
Services, | ||
get_chat_completion_service_and_request_settings, | ||
) | ||
from semantic_kernel import Kernel | ||
from semantic_kernel.connectors.ai.function_calling_utils import ( | ||
kernel_function_metadata_to_function_call_format, | ||
) | ||
from semantic_kernel.connectors.ai.open_ai.prompt_execution_settings.open_ai_prompt_execution_settings import ( | ||
OpenAIChatPromptExecutionSettings, | ||
) | ||
from semantic_kernel.contents import ChatHistory | ||
from semantic_kernel.contents.function_call_content import FunctionCallContent | ||
from semantic_kernel.contents.function_result_content import FunctionResultContent | ||
from semantic_kernel.core_plugins.time_plugin import TimePlugin | ||
|
||
""" | ||
# Reasoning Models Sample | ||
|
||
This sample demonstrates an example of how to use reasoning models such as OpenAI’s o1 and o1-mini for inference. | ||
Reasoning models currently have certain limitations, which are outlined below. | ||
|
||
1. Requires API version `2024-09-01-preview` or later. | ||
- `reasoning_effort` and `developer_message` are only supported in API version `2024-12-01-preview` or later. | ||
- o1-mini is not supported property `developer_message` `reasoning_effort` now. | ||
2. Developer message must be used instead of system message | ||
3. Parallel tool invocation is currently not supported | ||
4. Token limit settings need to consider both reasoning and completion tokens | ||
|
||
# Unsupported Properties ⛔ | ||
|
||
The following parameters are currently not supported: | ||
- temperature | ||
- top_p | ||
- presence_penalty | ||
- frequency_penalty | ||
- logprobs | ||
- top_logprobs | ||
- logit_bias | ||
- max_tokens | ||
- stream | ||
- tool_choice | ||
|
||
# Unsupported Roles ⛔ | ||
- system | ||
- tool | ||
|
||
# .env examples | ||
|
||
OpenAI: semantic_kernel/connectors/ai/open_ai/settings/open_ai_settings.py | ||
|
||
```.env | ||
OPENAI_API_KEY=******************* | ||
OPENAI_CHAT_MODEL_ID=o1-2024-12-17 | ||
``` | ||
|
||
Azure OpenAI: semantic_kernel/connectors/ai/open_ai/settings/azure_open_ai_settings.py | ||
|
||
```.env | ||
AZURE_OPENAI_API_KEY=******************* | ||
AZURE_OPENAI_ENDPOINT=https://*********.openai.azure.com | ||
AZURE_OPENAI_CHAT_DEPLOYMENT_NAME=o1-2024-12-17 | ||
AZURE_OPENAI_API_VERSION="2024-12-01-preview" | ||
``` | ||
|
||
Note: Unsupported features may be added in future updates. | ||
""" | ||
|
||
chat_completion_service, request_settings = get_chat_completion_service_and_request_settings(Services.OPENAI) | ||
|
||
# This is the system message that gives the chatbot its personality. | ||
developer_message = """ | ||
As an assistant supporting the user, | ||
you recognize all user input | ||
as questions or consultations and answer them. | ||
""" | ||
|
||
# Create a ChatHistory object | ||
chat_history = ChatHistory() | ||
|
||
# Create a kernel and register plugin. | ||
kernel = Kernel() | ||
kernel.add_plugin(TimePlugin(), "time") | ||
|
||
|
||
async def chat() -> bool: | ||
try: | ||
user_input = input("User:> ") | ||
except KeyboardInterrupt: | ||
print("\n\nExiting chat...") | ||
return False | ||
except EOFError: | ||
print("\n\nExiting chat...") | ||
return False | ||
|
||
if user_input == "exit": | ||
print("\n\nExiting chat...") | ||
return False | ||
|
||
# The developer message was newly introduced for reasoning models such as OpenAI’s o1 and o1-mini. | ||
# `system message` cannot be used with reasoning models. | ||
chat_history.add_developer_message(developer_message) | ||
chat_history.add_user_message(user_input) | ||
|
||
if not isinstance(request_settings, OpenAIChatPromptExecutionSettings): | ||
raise ValueError(f"{type(request_settings).__name__} settings are not supported for this sample.") | ||
|
||
# Set the reasoning effort to "medium" and the maximum completion tokens to 5000. | ||
request_settings.max_completion_tokens = 5000 | ||
request_settings.reasoning_effort = "medium" | ||
|
||
# enable the function calling and disable parallel tool calls for reasoning models. | ||
request_settings.parallel_tool_calls = None | ||
request_settings.tool_choice = None | ||
request_settings.tools = [ | ||
kernel_function_metadata_to_function_call_format(f) for f in kernel.get_full_list_of_function_metadata() | ||
] | ||
|
||
# Get the chat message content from the chat completion service. | ||
response = await chat_completion_service.get_chat_message_content( | ||
chat_history=chat_history, | ||
settings=request_settings, | ||
kernel=kernel, | ||
) | ||
|
||
if not response: | ||
return True | ||
|
||
function_calls = [item for item in response.items if isinstance(item, FunctionCallContent)] | ||
if len(function_calls) == 0: | ||
print(f"Mosscap:> {response}") | ||
chat_history.add_message(response) | ||
return True | ||
|
||
# Invoke the function calls and update the chat history with the results. | ||
print(f"processing {len(function_calls)} tool calls") | ||
await asyncio.gather( | ||
*[ | ||
kernel.invoke_function_call( | ||
function_call=function_call, | ||
chat_history=chat_history, | ||
function_call_count=len(function_calls), | ||
request_index=0, | ||
) | ||
for function_call in function_calls | ||
], | ||
) | ||
|
||
# Convert the last tool message to a user message. | ||
fc_results = [item for item in chat_history.messages[-1].items if isinstance(item, FunctionResultContent)] | ||
|
||
result_prompt: list[str] = ["FUNCTION CALL RESULT"] | ||
for fc_result in fc_results: | ||
result_prompt.append(f"- {fc_result.plugin_name}: {fc_result.result}") | ||
|
||
chat_history.remove_message(chat_history.messages[-1]) | ||
chat_history.add_user_message("\n".join(result_prompt)) | ||
print("Tools:> ", "\n".join(result_prompt)) | ||
|
||
# Get the chat message content from the chat completion service. | ||
request_settings.tools = None | ||
response = await chat_completion_service.get_chat_message_content( | ||
chat_history=chat_history, | ||
settings=request_settings, | ||
) | ||
|
||
# Add the chat message to the chat history to keep track of the conversation. | ||
if response: | ||
print(f"Mosscap:> {response}") | ||
chat_history.add_message(response) | ||
|
||
return True | ||
|
||
|
||
async def main() -> None: | ||
# Start the chat loop. The chat loop will continue until the user types "exit". | ||
chatting = True | ||
while chatting: | ||
chatting = await chat() | ||
|
||
# Sample output: | ||
# User:> What time is it? | ||
# Tools:> FUNCTION CALL RESULT | ||
# - time: Thursday, January 09, 2025 05:41 AM | ||
# Mosscap:> The current time is 05:41 AM. | ||
|
||
|
||
if __name__ == "__main__": | ||
asyncio.run(main()) |
This file contains 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
This file contains 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
Oops, something went wrong.
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 is great, thanks for adding the sample. Can we please update the concept sample README with this new "reasoning" category? https://github.com/microsoft/semantic-kernel/blob/main/python/samples/concepts/README.md
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.
@moonbox3
ok. Is it like this? : fd3e0dc#diff-f5e406ce9b1b9d01d54d2380c88086923087415cda950c90bbee37020c856a03R25