Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions openai_sample/activities/basic_model_activity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from temporalio import workflow
from openai import AsyncOpenAI
from temporalio import activity
with workflow.unsafe.imports_passed_through():
from braintrust import wrap_openai, init_logger


@activity.defn
async def basic_model_invocation(prompt: str) -> str:
client = wrap_openai(AsyncOpenAI())
logger=init_logger(project="Temporal-first-project")
response = await client.responses.create(
model="gpt-4o",
instructions="You are a coding assistant that talks like a pirate.",
input=prompt,
)
return response.output_text
26 changes: 26 additions & 0 deletions openai_sample/run_basic_model_workflow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import asyncio

from temporalio.client import Client

from workflows.basic_model_workflow import BasicModelWorkflow


async def main():
# Create client connected to server at the given address
client = await Client.connect(
"localhost:7233",
)

# Execute a workflow
result = await client.execute_workflow(
BasicModelWorkflow.run,
args=["How do I check if a Python object is an instance of a class?"],
id="basic-model-workflow",
task_queue="openai-basic-task-queue",
)

print(f"Result: {result}")


if __name__ == "__main__":
asyncio.run(main())
34 changes: 34 additions & 0 deletions openai_sample/run_worker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from __future__ import annotations

import asyncio

from temporalio.client import Client
from temporalio.contrib.opentelemetry import TracingInterceptor
from temporalio.worker import Worker

from activities.basic_model_activity import basic_model_invocation
from workflows.basic_model_workflow import BasicModelWorkflow


async def main():
# Create client connected to server at the given address
client = await Client.connect(
"localhost:7233",
)

worker = Worker(
client,
task_queue="openai-basic-task-queue",
workflows=[
BasicModelWorkflow,
],
activities=[
basic_model_invocation,
],
interceptors=[TracingInterceptor()]
)
await worker.run()


if __name__ == "__main__":
asyncio.run(main())
27 changes: 27 additions & 0 deletions openai_sample/workflows/basic_model_workflow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from __future__ import annotations

from datetime import timedelta

from temporalio import workflow

from openai_sample.activities.basic_model_activity import basic_model_invocation

with workflow.unsafe.imports_passed_through():
from braintrust import traced, init_logger

logger=init_logger(project="Temporal-first-project")

@workflow.defn(sandboxed=False)
class BasicModelWorkflow:
@traced
@workflow.run
async def run(self, question: str) -> str:
return await self.do_model(question)

@traced
async def do_model(self, question: str) -> str:
return await workflow.execute_activity(
basic_model_invocation,
question,
start_to_close_timeout=timedelta(minutes=1),
)
5 changes: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ authors = [{ name = "Temporal Technologies Inc", email = "[email protected]" }]
requires-python = ">=3.10"
readme = "README.md"
license = "MIT"
dependencies = ["temporalio>=1.18.0,<2"]
dependencies = [
"braintrust>=0.3.0",
"temporalio>=1.18.0,<2",
]

[project.urls]
Homepage = "https://github.com/temporalio/samples-python"
Expand Down
Loading
Loading