-
Notifications
You must be signed in to change notification settings - Fork 94
add example for gpt-oss #232
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
Merged
Merged
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
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,58 @@ | ||
| import asyncio | ||
| import logging | ||
| from datetime import timedelta | ||
| from typing import Optional | ||
|
|
||
| from agents import Model, ModelProvider, OpenAIChatCompletionsModel | ||
| from openai import AsyncOpenAI | ||
| from temporalio.client import Client | ||
| from temporalio.contrib.openai_agents import ModelActivityParameters, OpenAIAgentsPlugin | ||
| from temporalio.worker import Worker | ||
|
|
||
| from openai_agents.model_providers.workflows.gpt_oss_workflow import GptOssWorkflow | ||
|
|
||
| ollama_client = AsyncOpenAI( | ||
| base_url="http://localhost:11434/v1", # Local Ollama API endpoint | ||
| api_key="ollama", # Ignored by Ollama | ||
| ) | ||
|
|
||
|
|
||
| class CustomModelProvider(ModelProvider): | ||
| def get_model(self, model_name: Optional[str]) -> Model: | ||
| model = OpenAIChatCompletionsModel( | ||
| model=model_name if model_name else "gpt-oss:20b", | ||
| openai_client=ollama_client, | ||
| ) | ||
| return model | ||
|
|
||
|
|
||
| async def main(): | ||
| # Configure logging to show workflow debug messages | ||
| logging.basicConfig(level=logging.WARNING) | ||
| logging.getLogger("temporalio.workflow").setLevel(logging.DEBUG) | ||
|
|
||
| # Create client connected to server at the given address | ||
| client = await Client.connect( | ||
| "localhost:7233", | ||
| plugins=[ | ||
| OpenAIAgentsPlugin( | ||
| model_params=ModelActivityParameters( | ||
| start_to_close_timeout=timedelta(seconds=30) | ||
| ), | ||
| model_provider=CustomModelProvider(), | ||
| ), | ||
| ], | ||
| ) | ||
|
|
||
| worker = Worker( | ||
| client, | ||
| task_queue="openai-agents-model-providers-task-queue", | ||
| workflows=[ | ||
| GptOssWorkflow, | ||
| ], | ||
| ) | ||
| await worker.run() | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| asyncio.run(main()) | ||
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,27 @@ | ||
| import asyncio | ||
|
|
||
| from temporalio.client import Client | ||
| from temporalio.contrib.openai_agents import OpenAIAgentsPlugin | ||
|
|
||
| from openai_agents.model_providers.workflows.gpt_oss_workflow import GptOssWorkflow | ||
|
|
||
|
|
||
| async def main(): | ||
| client = await Client.connect( | ||
| "localhost:7233", | ||
| plugins=[ | ||
| OpenAIAgentsPlugin(), | ||
| ], | ||
| ) | ||
|
|
||
| result = await client.execute_workflow( | ||
| GptOssWorkflow.run, | ||
| "What's the weather in Tokyo?", | ||
| id="litellm-gpt-oss-workflow-id", | ||
| task_queue="openai-agents-model-providers-task-queue", | ||
| ) | ||
| print(f"Result: {result}") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| asyncio.run(main()) |
File renamed without changes.
26 changes: 26 additions & 0 deletions
26
openai_agents/model_providers/workflows/gpt_oss_workflow.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,26 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from agents import Agent, Runner, function_tool, set_tracing_disabled | ||
| from temporalio import workflow | ||
|
|
||
|
|
||
| @workflow.defn | ||
| class GptOssWorkflow: | ||
| @workflow.run | ||
| async def run(self, prompt: str) -> str: | ||
| set_tracing_disabled(disabled=True) | ||
|
|
||
| @function_tool | ||
| def get_weather(city: str): | ||
| workflow.logger.debug(f"Getting weather for {city}") | ||
| return f"The weather in {city} is sunny." | ||
|
|
||
| agent = Agent( | ||
| name="Assistant", | ||
| instructions="You only respond in haikus. When asked about the weather always use the tool to get the current weather..", | ||
| model="gpt-oss:20b", | ||
| tools=[get_weather], | ||
| ) | ||
|
|
||
| result = await Runner.run(agent, prompt) | ||
| return result.final_output |
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.
Client needs to be created outside of
CustomModelProvideras otherwise we have sandboxing issues: