|
| 1 | +import asyncio |
| 2 | +import logging |
| 3 | +from datetime import timedelta |
| 4 | +from typing import Optional |
| 5 | + |
| 6 | +from agents import Model, ModelProvider, OpenAIChatCompletionsModel |
| 7 | +from openai import AsyncOpenAI |
| 8 | +from temporalio.client import Client |
| 9 | +from temporalio.contrib.openai_agents import ModelActivityParameters, OpenAIAgentsPlugin |
| 10 | +from temporalio.worker import Worker |
| 11 | + |
| 12 | +from openai_agents.model_providers.workflows.gpt_oss_workflow import GptOssWorkflow |
| 13 | + |
| 14 | +ollama_client = AsyncOpenAI( |
| 15 | + base_url="http://localhost:11434/v1", # Local Ollama API endpoint |
| 16 | + api_key="ollama", # Ignored by Ollama |
| 17 | +) |
| 18 | + |
| 19 | + |
| 20 | +class CustomModelProvider(ModelProvider): |
| 21 | + def get_model(self, model_name: Optional[str]) -> Model: |
| 22 | + model = OpenAIChatCompletionsModel( |
| 23 | + model=model_name if model_name else "gpt-oss:20b", |
| 24 | + openai_client=ollama_client, |
| 25 | + ) |
| 26 | + return model |
| 27 | + |
| 28 | + |
| 29 | +async def main(): |
| 30 | + # Configure logging to show workflow debug messages |
| 31 | + logging.basicConfig(level=logging.WARNING) |
| 32 | + logging.getLogger("temporalio.workflow").setLevel(logging.DEBUG) |
| 33 | + |
| 34 | + # Create client connected to server at the given address |
| 35 | + client = await Client.connect( |
| 36 | + "localhost:7233", |
| 37 | + plugins=[ |
| 38 | + OpenAIAgentsPlugin( |
| 39 | + model_params=ModelActivityParameters( |
| 40 | + start_to_close_timeout=timedelta(seconds=30) |
| 41 | + ), |
| 42 | + model_provider=CustomModelProvider(), |
| 43 | + ), |
| 44 | + ], |
| 45 | + ) |
| 46 | + |
| 47 | + worker = Worker( |
| 48 | + client, |
| 49 | + task_queue="openai-agents-model-providers-task-queue", |
| 50 | + workflows=[ |
| 51 | + GptOssWorkflow, |
| 52 | + ], |
| 53 | + ) |
| 54 | + await worker.run() |
| 55 | + |
| 56 | + |
| 57 | +if __name__ == "__main__": |
| 58 | + asyncio.run(main()) |
0 commit comments