Skip to content

Commit

Permalink
ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
jxnl committed Dec 25, 2023
1 parent 4b29ffd commit a3ffe02
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions examples/union/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,37 @@
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]
Expand All @@ -35,6 +42,7 @@ def process(self):
"""Process the action."""
return self.action.process()


try:
# Enables `response_model`
client = instructor.patch(OpenAI())
Expand All @@ -43,10 +51,9 @@ def process(self):
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}")

0 comments on commit a3ffe02

Please sign in to comment.