Created
January 20, 2025 14:18
-
-
Save bemijonathan/bce152c72f5ec47426de2ddda13a6536 to your computer and use it in GitHub Desktop.
JSON_agent_py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import json | |
| import time | |
| quesitons = [ | |
| "What is the area of a circle with radius of 1?", | |
| # "What is the temperature in capital of United States of America?", | |
| "I would like to learn more about Albert Einstein?", | |
| "Does test-time chain-of-thought require special model training?" | |
| ] | |
| for question in quesitons: | |
| chat_history = [] # move history here | |
| chat_history.append({ | |
| "role": "user", | |
| "content": question | |
| }) | |
| while True: # add this line | |
| time.sleep(2) | |
| print(chat_history) | |
| completion = client.chat.completions.create( | |
| model="meta-llama/Llama-3.3-70B-Instruct-Turbo-Free", | |
| messages=chat_history, | |
| tools=tools | |
| ) | |
| tool_calls = completion.choices[0].message.tool_calls | |
| print(completion.choices[0].message) | |
| if tool_calls: | |
| arguments = tool_calls[0].function.arguments | |
| print(arguments) | |
| name = tool_calls[0].function.name | |
| print(name) | |
| chat_history.append(completion.choices[0].message) | |
| if name == "get_weather": | |
| city = json.loads(arguments)["city_name"] | |
| weather_description = get_weather(city) | |
| print(f"Weather description: {weather_description}") | |
| print() | |
| chat_history.append({ | |
| "role": "function", | |
| "name": name, | |
| "content": json.dumps(weather_data) | |
| }) | |
| elif name == "get_wiki_article": | |
| subject = json.loads(arguments)["subject"] | |
| wiki_summary = get_wiki_summary(subject) | |
| print(f"Wiki results: {wiki_summary}") | |
| print() | |
| chat_history.append({ | |
| "role": "function", | |
| "name": name, | |
| "content": json.dumps(wiki_summary) | |
| }) | |
| break # because research is infinite | |
| elif name == "python_intepretter": | |
| python_code = json.loads(arguments)["python_code"] | |
| print("Python results:") | |
| output = execute_python(python_code) | |
| print() | |
| chat_history.append({ | |
| "role": "function", | |
| "name": name, | |
| "content": json.dumps(output) | |
| }) | |
| else: | |
| break # add this line | |
| else: | |
| chat_history.append({ | |
| "role": "assistant", | |
| "content": f"No tool usage: {completion.choices[0].message.content}" | |
| }) | |
| print(f"No tool usage: {completion.choices[0].message.content}") | |
| break # add this line |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment