_j
April 10, 2024, 12:20am
9
Here’s the basics documentation of functions:
https://platform.openai.com/docs/guides/function-calling
Since they don’t explicitly demonstrate how to pass the assistant function call and the function return back when using functions, I wrote that up:
Hey folks. I’m about to show how to properly insert your function return back to the AI - something far too useful for OpenAI to document properly.
chat_completion_parameters = {
"model": "gpt-3.5-turbo",
"top_p": 0.5,
"messages": [
{"role": "system", "content": "You are MegaBot, my fine-tune AI identity."},
# chat history goes here
{"role": "user", "content": "Who won the 2024 election?"},
{"role": "assistant", "content": assistant_content_if_exist,
"function_call": {
"name": called_functi…
Then if you were to use tools instead of functions to make use of functions, the specification and the handling is different. I also show how to do the assistant + return response, in code:
I decided to make the mega-demo. Going beyond this I might as well just write a chatbot with classes for handling simulated functions and showing rewriting them to a real API…
# imports and set up the OpenAI client object with a shorter timeout
from openai import OpenAI
import json
client = OpenAI(timeout=30)
# Here we'll make a tool specification, more flexible by adding one at a time
toolspec=[]
toolspec.extend([{
"type": "function",
"function": {
"name": "get_we…