Yeah sure. Thanks for the reply!
## GPT Call to get Book Data
from openai import OpenAI
import os
api_key = os.environ.get('open_ai')
def gpt_get_book_data(message_containing_book_info,):
client = OpenAI(api_key=api_key)
system_role = (
"You are a bot that gets book data from an API using the title and author as arguments "
"You read a body of text, extract all titles and authors, and use them as arguments for a function. "
"You are to use the function 'get_book_data' "
"\n"
)
additional_system_role_attributes = (
#f"The current date is {current_date}\n"
"There may be multiple titles and authors in the text. Make sure you get tham all\n"
)
book_functions = [
{
"type": "function",
"function": {
"name": "get_book_data",
"description":"Gets book data from open library API",
"parameters":{
"type": "object",
"properties": {
"title": {
"type":"string",
"description":"The title of a book"
},
"author": {
"type":"string",
"description":"the author of a book"
},
},
"required":["title","author"]
},
}
}
]
chat_history = [
{"role": "system", "content": f"{system_role}{additional_system_role_attributes}"},
{"role": "user", "content": f"{message_containing_book_info}"}
]
response = client.chat.completions.create(
model="gpt-3.5-turbo-0125",
messages=chat_history,
tools=book_functions,
tool_choice= {"type": "function", "function": {"name": "get_book_data"} },
temperature=0,
)
return response
book_message = (
'"Dune" by Frank Herbert is a mesmerizing tale set on the desert planet of Arrakis, known for its valuable spice melange. '
'The story follows the noble family of House Atreides as they navigate political intrigue, navigate treacherous environments, '
'and encounter complex characters in a richly detailed universe.\n\n'
'"The Three-Body Problem" by Liu Cixin delves into the mysteries of physics and the complexities of human nature when alien '
'contact disrupts the world. The narrative unfolds through a blend of historical events, intricate scientific theories, and a thrilling plot '
'that keeps readers on the edge of their seats. A must-read for sci-fi enthusiasts craving a unique and thought-provoking experience.'
)
response = gpt_get_book_data(message_containing_book_info=book_message)
This returns
ChatCompletion(id='chatcmpl-9APi8g0QKneuEqn0ubMK1RAuFS7kE', choices=[Choice(finish_reason='stop', index=0, logprobs=None, message=ChatCompletionMessage(content=None, role='assistant', function_call=None, tool_calls=[ChatCompletionMessageToolCall(id='call_oS2Palu6R5fpI2ZWLzz5Jt4s', function=Function(arguments='{"title":"Dune","author":"Frank Herbert"}', name='get_book_data'), type='function')]))], created=1712269140, model='gpt-3.5-turbo-0125', object='chat.completion', system_fingerprint='fp_b28b39ffa8', usage=CompletionUsage(completion_tokens=11, prompt_tokens=286, total_tokens=297))
As you can see in the book_message string, there are 2 titles. However, only one is recognized and used as an argument pair. I haven’t messed around with the prompt too much. So maybe that’s the issue?
Additionally, I have tried to do it without the tool_choice argument, all else equal and it successfully recognizes both titles ~70-85% of the time. This is okay but not sufficient for my purposes.
Thanks again!