mirror of
https://github.com/zhxycn/Telegram-ChatBot-Serverless.git
synced 2024-11-24 15:59:22 +00:00
✨ Show chat information
This commit is contained in:
parent
d0c866874b
commit
51ff8df7c5
@ -51,7 +51,16 @@ def process_event(event):
|
||||
text = text.split(" ", 1)[1]
|
||||
messages = [{"role": "user", "content": text}]
|
||||
bot.send_chat_action(chat_id, "typing")
|
||||
response = escape_special_characters(create_conversation(messages))
|
||||
conversation = create_conversation(messages)
|
||||
content = escape_special_characters(conversation[0])
|
||||
model = conversation[1]
|
||||
tokens = conversation[2]
|
||||
response = (
|
||||
f"{content}\n\n"
|
||||
f"\-\-\-\-\-\-\-\-\-\- *Information* \-\-\-\-\-\-\-\-\-\-\n"
|
||||
f"Model: `{model}`\n"
|
||||
f"Tokens: `{tokens[0]}({tokens[1]}\+{tokens[2]})`"
|
||||
)
|
||||
bot.reply_message(chat_id, message_id, response)
|
||||
except IndexError:
|
||||
bot.reply_message(chat_id, message_id, "请输入对话内容")
|
||||
|
@ -5,10 +5,17 @@ import openai
|
||||
openai.api_key = os.environ["OPENAI_API_KEY"]
|
||||
|
||||
|
||||
def create_conversation(messages: list) -> str:
|
||||
def create_conversation(messages: list):
|
||||
response = openai.ChatCompletion.create(
|
||||
model="gpt-3.5-turbo",
|
||||
messages=messages,
|
||||
temperature=1,
|
||||
)
|
||||
return response["choices"][0]["message"]["content"]
|
||||
content = response["choices"][0]["message"]["content"]
|
||||
model = response["model"]
|
||||
tokens = [
|
||||
response["usage"]["total_tokens"],
|
||||
response["usage"]["prompt_tokens"],
|
||||
response["usage"]["completion_tokens"],
|
||||
]
|
||||
return content, model, tokens
|
||||
|
Loading…
Reference in New Issue
Block a user