-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.py
More file actions
82 lines (64 loc) · 3.01 KB
/
Copy pathClient.py
File metadata and controls
82 lines (64 loc) · 3.01 KB
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
import tkinter as tk
from tkinter import scrolledtext
import openai
import tiktoken
# Set your OpenAI API key here
openai.api_key = "YOUR API KEY HERE"
# Function to count the tokens in a message
def count_tokens(message, model="gpt-4"):
encoding = tiktoken.encoding_for_model(model)
return len(encoding.encode(message))
# Function to handle sending messages to the OpenAI API
def send_message():
user_message = user_input.get("1.0", tk.END).strip()
if user_message:
# Count the tokens in the user's message
user_tokens = count_tokens(user_message)
chat_history.config(state=tk.NORMAL)
chat_history.insert(tk.END, "You: " + user_message + " (Tokens used: ")
token_start_index = chat_history.index(tk.END) # Get the current position
chat_history.insert(tk.END, str(user_tokens))
token_end_index = chat_history.index(tk.END) # Get the position after inserting token count
chat_history.insert(tk.END, ")\n\n")
# Highlight the token count in red
chat_history.tag_add("token_highlight", token_start_index, token_end_index)
chat_history.tag_configure("token_highlight", foreground="red")
chat_history.config(state=tk.DISABLED)
user_input.delete("1.0", tk.END)
# Append user message to conversation history
conversation_history.append({"role": "user", "content": user_message})
# OpenAI API call using gpt-4-turbo
response = openai.ChatCompletion.create(
model="gpt-4o-2024-05-13",
#Add your own model!
messages=conversation_history,
max_tokens=4096,
temperature=0.9,
top_p=1,
frequency_penalty=0,
presence_penalty=0.6
)
ai_message = response.choices[0].message['content'].strip()
# Append AI response to conversation history
conversation_history.append({"role": "assistant", "content": ai_message})
chat_history.config(state=tk.NORMAL)
chat_history.insert(tk.END, "AI: " + ai_message + "\n\n")
chat_history.config(state=tk.DISABLED)
# Initialize conversation history
conversation_history = [
{"role": "system", "content": "You are an AI assistant. The assistant is helpful, creative, clever, and very friendly."}
]
# GUI setup
root = tk.Tk()
root.title("Chatbot")
root.configure(bg='black')
# Apply font settings
font_family = "Comfortaa"
font_size = 20
chat_history = scrolledtext.ScrolledText(root, state=tk.DISABLED, wrap=tk.WORD, bg='black', fg='lime', font=(font_family, font_size))
chat_history.pack(padx=10, pady=10, fill=tk.BOTH, expand=True)
user_input = tk.Text(root, height=3, bg='black', fg='lime', insertbackground='white', font=(font_family, font_size))
user_input.pack(padx=10, pady=(0, 10), fill=tk.X)
send_button = tk.Button(root, text="Send", command=send_message, bg='black', fg='white', font=(font_family, font_size))
send_button.pack(pady=(0, 10))
root.mainloop()