Calling AI Functions #10
Replies: 4 comments 1 reply
-
What bugs me the most is that even the examples in the documentation doesn't seem to work. The print in the function never gets called, and this is the return of the message: role=<ChatRole.ASSISTANT: 'assistant'> content="I'm sorry, I am an AI language model and I do not have real-time data. To find out the current weather in Buenos Aires, you can check a reliable weather website or use a weather app for the most up-to-date information." name=None function_call=None
|
Beta Was this translation helpful? Give feedback.
-
Update: Calling chat_round_str does not call AI Functions. Calling the example from the documentation like below does indeed call AI functions.
I guess the only question is, is there a WAY to use pydantic objects to define the payload of the funcitons? |
Beta Was this translation helpful? Give feedback.
-
Update: Code below uses the example inclusive Pydantic objects.
|
Beta Was this translation helpful? Give feedback.
-
Hey @nicolascepeda - Thanks for the kind words! So the code you had originally was fine, only issue was using A full code example (using from pydantic import BaseModel, Field
from kani import Kani, ai_function, full_round
from kani.engines.openai import OpenAIEngine
class Sentiment(BaseModel):
sentiment: str = Field(description="Sentiment of the review")
sentiment_score: int = Field(description="Score between 1-100 of the sentiment")
class MyKani(Kani):
@ai_function(json_schema = Sentiment.model_json_schema(), after= ChatRole.USER)
def analyze_sentiment(self, **kwargs):
""" Output sentiment of review as a pydantic object """
return Sentiment.model_validate(kwargs)
engine = OpenAIEngine(os.environ["OPENAI_API_KEY"], model="gpt-3.5-turbo", temperature=0)
ai = MyKani(engine, system_prompt="You are a helpful review analysis tool.")
review = "..."
query = f"{review}\nAnalyze the sentiment of the review above"
messages = [item async for item in ai.full_round(query)] Then parse the messages to get the return of the function call As a quick note, your example for |
Beta Was this translation helpful? Give feedback.
-
Hi,
thanks for the great work with Kani. I decided to use it on my AI projects as it is a very modulare, low on boilerplate & very well structured project. Great Kudos for the work so far.
I have a question, I am trying to use AI functions to retunr structured JSON. This is the code I am using calling directly OpenAI api:
I don't manage to have kani calling the same APIs. I want to be able to define the schema of the function as a Pydantic object. Is this somethign that is not supported at all at the moment? I have tryied everything but the function doesn't get called.
Running out of ideas, any inputs would be highly appreciated. Thanks!
Beta Was this translation helpful? Give feedback.
All reactions