Files
wehub-resource-sync 97e91a83f3
Ruff / Ruff (push) Waiting to run
Test / Core Tests (push) Waiting to run
Test / Offline Coverage Tests (Python 3.10) (push) Waiting to run
Test / Offline Coverage Tests (Python 3.11) (push) Waiting to run
Test / Offline Coverage Tests (Python 3.12) (push) Waiting to run
Test / Offline Coverage Tests (Python 3.13) (push) Waiting to run
Test / Offline Coverage Tests (Python 3.9) (push) Waiting to run
Test / Full Coverage (Python 3.11) (push) Waiting to run
Test / Core Provider Tests (OpenAI) (push) Blocked by required conditions
Test / Core Provider Tests (Anthropic) (push) Blocked by required conditions
Test / Core Provider Tests (Google) (push) Blocked by required conditions
Test / Core Provider Tests (Other) (push) Blocked by required conditions
Test / Anthropic Tests (push) Blocked by required conditions
Test / Gemini Tests (push) Blocked by required conditions
Test / Google GenAI Tests (push) Blocked by required conditions
Test / Vertex AI Tests (push) Blocked by required conditions
Test / OpenAI Tests (push) Blocked by required conditions
Test / Writer Tests (push) Blocked by required conditions
Test / Auto Client Tests (push) Blocked by required conditions
ty / type-check (push) Waiting to run
chore: import upstream snapshot with attribution
2026-07-13 13:36:38 +08:00

60 lines
1.6 KiB
Python

from pydantic import BaseModel, Field
from typing import Union
import instructor
from openai import OpenAI
class Search(BaseModel):
"""Search action class with a 'query' field and a process method."""
query: str = Field(description="The search query")
def process(self):
"""Process the search action."""
return f"Search method called for query: {self.query}"
class Lookup(BaseModel):
"""Lookup action class with a 'keyword' field and a process method."""
keyword: str = Field(description="The lookup keyword")
def process(self):
"""Process the lookup action."""
return f"Lookup method called for keyword: {self.keyword}"
class Finish(BaseModel):
"""Finish action class with an 'answer' field and a process method."""
answer: str = Field(description="The answer for finishing the process")
def process(self):
"""Process the finish action."""
return f"Finish method called with answer: {self.answer}"
# Union of Search, Lookup, and Finish
class TakeAction(BaseModel):
action: Union[Search, Lookup, Finish]
def process(self):
"""Process the action."""
return self.action.process()
try:
# Enables `response_model`
client = instructor.from_openai(OpenAI())
action = client.chat.completions.create(
model="gpt-3.5-turbo",
response_model=TakeAction,
messages=[
{"role": "user", "content": "Please choose one action"},
],
)
assert isinstance(action, TakeAction), "The action is not TakeAction"
print(action.process())
except Exception as e:
print(f"An error occurred: {e}")