-
Notifications
You must be signed in to change notification settings - Fork 1
/
agent.py
146 lines (126 loc) · 4.83 KB
/
agent.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import os
import json
from dotenv import load_dotenv
from openai import OpenAI
from config import *
from utils import read_from_file, write_to_file, list_files, create_directory, delete_file
from tools import tools
load_dotenv()
client = OpenAI(api_key=os.getenv('OPENAI_API_KEY'))
def calculate_cost(response, model):
prompt_tokens = response.usage.prompt_tokens
completion_tokens = response.usage.completion_tokens
if 'gpt-4o-mini' in model:
# gpt-40-min pricing
input_cost = prompt_tokens * 0.15 / 1000000
output_cost = completion_tokens * 0.6 / 1000000
elif 'gpt-4o' in model:
# gpt-4o pricing
input_cost = prompt_tokens * 5.00 / 1000000
output_cost = completion_tokens * 15.00 / 1000000
else:
# GPT-3 pricing
input_cost = prompt_tokens * 0.50 / 1000000
output_cost = completion_tokens * 1.50 / 1000000
total_cost = input_cost + output_cost
return total_cost
def cost_color(total_cost):
if total_cost < 0.01:
return LIGHT_GREEN + f"${total_cost:.6f}" + RESET
elif total_cost < 0.10:
return LIGHT_YELLOW + f"${total_cost:.6f}" + RESET
else:
return RED + f"⚠️ ${total_cost:.6f}" + RESET
def run_conversation(message, messages, model="gpt-4o-mini"):
messages.append(message)
content = message['content'].strip().lower()
# Handling commands
if content.startswith('/'):
if content == '/gpt3':
model = "gpt-3.5-turbo"
print(LIGHT_GREEN + "Switched to GPT-3-turbo." + RESET)
elif content == '/gpt4-mini':
model = "gpt-4o-mini"
print(LIGHT_GREEN + "Switched to gpt-4o-mini." + RESET)
elif content == '/gpt4':
model = "gpt-4o"
print(LIGHT_GREEN + "Switched to gpt-4o." + RESET)
elif content == '/gpt':
print(LIGHT_GREEN + f"Current model is: {model}" + RESET)
else:
print(LIGHT_GREEN + "Invalid command." + RESET)
user_input = input(LIGHT_GREEN + "\nUSER: " + RESET)
message = {
"role": "user",
"content": user_input
}
run_conversation(message, messages, model)
return
if content == 'exit':
print(LIGHT_YELLOW + "GPT: Goodbye! 👋" + RESET)
return
response = client.chat.completions.create(
model=model,
messages=messages,
tools=tools,
tool_choice="auto",
)
response_message = response.choices[0].message
tool_calls = response_message.tool_calls
if tool_calls:
handle_tool_calls(tool_calls, messages, model, response_message)
else:
process_user_input(response, messages, model)
def handle_tool_calls(tool_calls, messages, model, response_message):
available_functions = {
"read_from_file": read_from_file,
"write_to_file": write_to_file,
"list_files": list_files,
"create_directory": create_directory,
"delete_file": delete_file,
}
messages.append(response_message)
for tool_call in tool_calls:
function_name = tool_call.function.name
function_to_call = available_functions[function_name]
function_args = json.loads(tool_call.function.arguments)
function_response = function_to_call(**function_args)
print(DARK_VIOLET + f"--> {function_name}" + RESET)
messages.append({
"tool_call_id": tool_call.id,
"role": "tool",
"name": function_name,
"content": function_response,
})
second_response = client.chat.completions.create(
model=model,
messages=messages,
)
process_user_input(second_response, messages, model)
def process_user_input(response, messages, model):
token_info = f"{response.usage.total_tokens} Tokens"
cost = calculate_cost(response, model)
cost_display = cost_color(cost)
user_input = input(DARK_YELLOW + "GPT:" + RESET + LIGHT_YELLOW + f" {response.choices[0].message.content} ({token_info} - {cost_display})\n" + DARK_GREEN + "USER: " + RESET + LIGHT_GREEN)
if user_input.strip().lower() == 'exit':
print(LIGHT_YELLOW + "GPT: Goodbye! 👋" + RESET)
return
else:
message = {
"role": "user",
"content": user_input
}
run_conversation(message, messages, model)
# Initial setup
messages = [
{
"role": "system",
"content": "You are an AI Assistant that can answer any question by using function calls. Answer the questions you are asked as good as possible and tell what you've done. Use a friendly tone and emojis to make the conversation more engaging. 😊"
}
]
user_message = input(DARK_YELLOW + "GPT: Hi! How may I assist you today? 😊\n" + RESET + LIGHT_GREEN + "USER: ")
message = {
"role": "user",
"content": user_message
}
run_conversation(message, messages)