├── README.md ├── agent.py └── gpt_functions.py /README.md: -------------------------------------------------------------------------------- 1 | # GPT Function Calling AI Agent Demo 2 | 3 | This is a simple demo of an AI agent using the OpenAI Function Calling. 4 | 5 | If you like the code, consider [buying me a coffee](https://buymeacoffee.com/unconv) 6 | 7 | ## Quick start 8 | 9 | ```console 10 | $ pip install --upgrade openai 11 | $ python3 agent.py 12 | ``` 13 | -------------------------------------------------------------------------------- /agent.py: -------------------------------------------------------------------------------- 1 | import openai 2 | import json 3 | import sys 4 | import os 5 | 6 | import gpt_functions 7 | 8 | openai.api_key = os.getenv("OPENAI_API_KEY") 9 | 10 | def parse_function_response(message): 11 | function_call = message["function_call"] 12 | function_name = function_call["name"] 13 | 14 | print("GPT: Called function " + function_name ) 15 | 16 | try: 17 | arguments = json.loads(function_call["arguments"]) 18 | 19 | if hasattr(gpt_functions, function_name): 20 | function_response = getattr(gpt_functions, function_name)(**arguments) 21 | else: 22 | function_response = "ERROR: Called unknown function" 23 | except: 24 | function_response = "ERROR: Invalid arguments" 25 | 26 | return (function_name, function_response) 27 | 28 | 29 | def run_conversation(message, messages = []): 30 | messages.append(message) 31 | 32 | with open("messages.json", "w") as f: 33 | f.write(json.dumps(messages, indent=4)) 34 | 35 | response = openai.ChatCompletion.create( 36 | messages=messages, 37 | model="gpt-3.5-turbo-0613", 38 | functions=gpt_functions.definitions, 39 | function_call="auto", 40 | ) 41 | 42 | message = response["choices"][0]["message"] 43 | messages.append(message) 44 | 45 | if "function_call" in message: 46 | function_name, function_response = parse_function_response(message) 47 | 48 | message = { 49 | "role": "function", 50 | "name": function_name, 51 | "content": function_response 52 | } 53 | else: 54 | user_message = input("GPT: " + message["content"] + "\nYou: ") 55 | message = { 56 | "role": "user", 57 | "content": user_message 58 | } 59 | 60 | run_conversation(message, messages) 61 | 62 | messages = [ 63 | { 64 | "role": "system", 65 | "content": "You are an AI bot that can do everything using function calls. When you are asked to do something, use the function call you have available and then respond with a message confirming what you have done." 66 | } 67 | ] 68 | 69 | user_message = input("GPT: What do you want to do?\nYou: ") 70 | message = { 71 | "role": "user", 72 | "content": user_message 73 | } 74 | 75 | run_conversation(message, messages) 76 | -------------------------------------------------------------------------------- /gpt_functions.py: -------------------------------------------------------------------------------- 1 | import json 2 | 3 | def write_file(filename, content): 4 | sure = input("Do you want to write to " + filename + "? (YES/NO) ") 5 | if sure == "YES": 6 | with open(filename, "w") as f: 7 | f.write(content) 8 | return "Successfully written file " + filename 9 | else: 10 | return "ERROR: You are not allowed to write to this file" 11 | 12 | def get_jokes(number_of_jokes): 13 | return json.dumps(["Why don't scientists trust atoms? Because they make up all the things!", 'How did the computer get wasted? It took screenshots!', "Why don't skeletons fight other skeletons? They don't have the guts!"]) 14 | 15 | definitions = [ 16 | { 17 | "name": "get_jokes", 18 | "description": "Gets jokes from the joke database", 19 | "parameters": { 20 | "type": "object", 21 | "properties": { 22 | "number_of_jokes": { 23 | "type": "number", 24 | "description": "Gets the specified number of jokes" 25 | } 26 | } 27 | }, 28 | "required": ["number_of_jokes"] 29 | }, 30 | { 31 | "name": "write_file", 32 | "description": "Writes content to a file", 33 | "parameters": { 34 | "type": "object", 35 | "properties": { 36 | "filename": { 37 | "type": "string", 38 | "description": "Filename to write to" 39 | }, 40 | "content": { 41 | "type": "string", 42 | "description": "Content to write to file" 43 | } 44 | } 45 | }, 46 | "required": ["filename", "content"] 47 | } 48 | ] 49 | --------------------------------------------------------------------------------