-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from sbslee/0.8.0-dev
0.8.0 dev
- Loading branch information
Showing
12 changed files
with
251 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import tkinter as tk | ||
import importlib.util | ||
import json | ||
|
||
import openai | ||
|
||
from .gui import Settings, Conversation | ||
from .utils import tokens2price | ||
|
||
class FuncGPT: | ||
def __init__( | ||
self, | ||
kanu, | ||
openai_key, | ||
model, | ||
temperature, | ||
prompt, | ||
function_script | ||
): | ||
self.kanu = kanu | ||
self.model = model | ||
self.temperature = temperature | ||
self.prompt = prompt | ||
self.function_script = function_script | ||
openai.api_key = openai_key | ||
self.settings = Settings(self) | ||
self.conversation = Conversation(self) | ||
self.tokens = 0 | ||
self.price = 0 | ||
self.module = importlib.machinery.SourceFileLoader("", self.function_script).load_module() | ||
|
||
def run(self): | ||
self.conversation.page() | ||
|
||
def send_message(self): | ||
if not self.messages: | ||
self.messages.append({"role": "system", "content": self.prompt}) | ||
self.messages += [{"role": "user", "content": self.user_input.get()}] | ||
bot_response = openai.ChatCompletion.create( | ||
model=self.model, | ||
messages=self.messages, | ||
temperature=self.temperature, | ||
functions=[x["json"] for x in self.module.functions.values()], | ||
function_call="auto", | ||
) | ||
message = bot_response["choices"][0]["message"] | ||
if message.get("function_call"): | ||
function_name = message["function_call"]["name"] | ||
function_args = json.loads(message["function_call"]["arguments"]) | ||
function_response = self.module.functions[function_name]["function"](**function_args) | ||
second_response = openai.ChatCompletion.create( | ||
model=self.model, | ||
messages=[ | ||
{"role": "user", "content": self.user_input.get()}, | ||
message, | ||
{ | ||
"role": "function", | ||
"name": function_name, | ||
"content": function_response, | ||
}, | ||
], | ||
) | ||
self.calculate_usage(second_response, function=function_name) | ||
response = second_response["choices"][0]["message"]["content"] | ||
else: | ||
response = bot_response["choices"][0]["message"]["content"] | ||
self.messages += [{"role": "assistant", "content": response}] | ||
self.session.insert(tk.END, "You: " + self.user_input.get() + "\n", "user") | ||
self.session.insert(tk.END, f"Bot: " + response + "\n", "bot") | ||
self.calculate_usage(bot_response) | ||
self.chatbox.delete(0, tk.END) | ||
|
||
def calculate_usage(self, response, function=None): | ||
total_tokens = response["usage"]["total_tokens"] | ||
prompt_tokens = response["usage"]["prompt_tokens"] | ||
completion_tokens = response["usage"]["completion_tokens"] | ||
prompt_price = tokens2price(self.model, "prompt", prompt_tokens) | ||
completion_price = tokens2price(self.model, "completion", completion_tokens) | ||
self.price += prompt_price + completion_price | ||
self.tokens += total_tokens | ||
if function is None: | ||
message = f"System: Used {prompt_tokens:,} prompt + {completion_tokens:,} completion = {total_tokens:,} tokens (total: {self.tokens:,} or ${self.price:.6f})." | ||
else: | ||
message = f"System: Used {prompt_tokens:,} prompt + {completion_tokens:,} completion = {total_tokens:,} tokens (total: {self.tokens:,} or ${self.price:.6f}) (called function: {function})." | ||
self.system.insert(tk.END, f"{message}\n", "system") | ||
|
||
def clear_session(self): | ||
self.tokens = self.price = 0 | ||
self.run() |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
__version__ = "0.7.0" | ||
__version__ = "0.8.0" |