|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import openai |
|
openai.ChatCompletion.create( |
|
model="gpt-3.5-turbo", |
|
messages=[ |
|
{"role": "system", "content": "You are a helpful assistant."}, |
|
{"role": "user", "content": "Who won the world series in 2020?"}, |
|
{"role": "assistant", "content": "The Los Angeles Dodgers won the World Series in 2020."}, |
|
{"role": "user", "content": "Where was it played?"} |
|
] |
|
) |
|
|
|
|
|
|
|
|
|
import openai |
|
import os |
|
|
|
OPENAI_API_KEY=os.environ.get("OPENAI_API_KEY") |
|
openai.api_key = OPENAI_API_KEY |
|
|
|
class Conversation: |
|
|
|
|
|
def __init__(self, prompt, num_of_round): |
|
self.prompt = prompt |
|
self.num_of_round = num_of_round |
|
self.messages = [] |
|
self.messages.append({"role": "system", "content": self.prompt}) |
|
|
|
|
|
|
|
|
|
def ask(self, question): |
|
try: |
|
self.messages.append( {"role": "user", "content": question}) |
|
response = openai.ChatCompletion.create( |
|
model="gpt-3.5-turbo", |
|
messages=self.messages, |
|
temperature=0.5, |
|
max_tokens=2048, |
|
top_p=1, |
|
) |
|
except Exception as e: |
|
print(e) |
|
return e |
|
|
|
message = response["choices"][0]["message"]["content"] |
|
self.messages.append({"role": "assistant", "content": message}) |
|
|
|
if len(self.messages) > self.num_of_round*2 + 1: |
|
del self.messages[1:3] |
|
return message |
|
|
|
|
|
|
|
|
|
|
|
prompt = """你是一个中国厨师,用中文回答做菜的问题。你的回答需要满足以下要求: |
|
1. 你的回答必须是中文 |
|
2. 回答限制在100个字以内""" |
|
conv1 = Conversation(prompt, 3) |
|
question1 = "你是谁?" |
|
print("User : %s" % question1) |
|
print("Assistant : %s\n" % conv1.ask(question1)) |
|
|
|
question2 = "请问鱼香肉丝怎么做?" |
|
print("User : %s" % question2) |
|
print("Assistant : %s\n" % conv1.ask(question2)) |
|
|
|
question3 = "那蚝油牛肉呢?" |
|
print("User : %s" % question3) |
|
print("Assistant : %s\n" % conv1.ask(question3)) |
|
|
|
|
|
|
|
|
|
|
|
question4 = "我问你的第一个问题是什么?" |
|
print("User : %s" % question4) |
|
print("Assistant : %s\n" % conv1.ask(question4)) |
|
|
|
|
|
|
|
|
|
|
|
question5 = "我问你的第一个问题是什么?" |
|
print("User : %s" % question5) |
|
print("Assistant : %s\n" % conv1.ask(question5)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Conversation2: |
|
def __init__(self, prompt, num_of_round): |
|
self.prompt = prompt |
|
self.num_of_round = num_of_round |
|
self.messages = [] |
|
self.messages.append({"role": "system", "content": self.prompt}) |
|
|
|
def ask(self, question): |
|
try: |
|
self.messages.append( {"role": "user", "content": question}) |
|
response = openai.ChatCompletion.create( |
|
model="gpt-3.5-turbo", |
|
messages=self.messages, |
|
temperature=0.5, |
|
max_tokens=2048, |
|
top_p=1, |
|
) |
|
except Exception as e: |
|
print(e) |
|
return e |
|
|
|
message = response["choices"][0]["message"]["content"] |
|
num_of_tokens = response['usage']['total_tokens'] |
|
self.messages.append({"role": "assistant", "content": message}) |
|
|
|
if len(self.messages) > self.num_of_round*2 + 1: |
|
del self.messages[1:3] |
|
return message, num_of_tokens |
|
|
|
|
|
|
|
|
|
|
|
conv2 = Conversation2(prompt, 3) |
|
questions = [question1, question2, question3, question4, question5] |
|
for question in questions: |
|
answer, num_of_tokens = conv2.ask(question) |
|
print("询问 {%s} 消耗的token数量是 : %d" % (question, num_of_tokens)) |
|
|
|
|
|
|
|
|
|
|
|
import tiktoken |
|
encoding = tiktoken.get_encoding("cl100k_base") |
|
|
|
conv2 = Conversation2(prompt, 3) |
|
question1 = "你是谁?" |
|
answer1, num_of_tokens = conv2.ask(question1) |
|
print("总共消耗的token数量是 : %d" % (num_of_tokens)) |
|
|
|
prompt_count = len(encoding.encode(prompt)) |
|
question1_count = len(encoding.encode(question1)) |
|
answer1_count = len(encoding.encode(answer1)) |
|
total_count = prompt_count + question1_count + answer1_count |
|
print("Prompt消耗 %d Token, 问题消耗 %d Token,回答消耗 %d Token,总共消耗 %d Token" % (prompt_count, question1_count, answer1_count, total_count)) |
|
|
|
|
|
|
|
system_start_count = len(encoding.encode("<|im_start|>system\n")) |
|
print(encoding.encode("<|im_start|>system\n")) |
|
end_count = len(encoding.encode("<|im_end|>\n")) |
|
print(encoding.encode("<|im_end|>\n")) |
|
user_start_count = len(encoding.encode("<|im_start|>user\n")) |
|
print(encoding.encode("<|im_start|>user\n")) |
|
assistant_start_count = len(encoding.encode("<|im_start|>assistant\n")) |
|
print(encoding.encode("<|im_start|>assistant\n")) |
|
|
|
total_mark_count = system_start_count + user_start_count + assistant_start_count + end_count*2 |
|
print("系统拼接的标记消耗 %d Token" % total_mark_count) |
|
|
|
|
|
|
|
|
|
|
|
get_ipython().run_line_magic('pip', 'install gradio') |
|
|
|
|
|
|
|
|
|
|
|
get_ipython().run_line_magic('pip', 'install --upgrade gradio') |
|
|
|
|
|
|
|
|
|
|
|
import gradio as gr |
|
prompt = """你是一个中国厨师,用中文回答做菜的问题。你的回答需要满足以下要求: |
|
1. 你的回答必须是中文 |
|
2. 回答限制在100个字以内""" |
|
|
|
conv = Conversation(prompt, 5) |
|
|
|
|
|
def predict(input, history=[]): |
|
history.append(input) |
|
response = conv.ask(input) |
|
history.append(response) |
|
|
|
responses = [(u,b) for u,b in zip(history[::2], history[1::2])] |
|
return responses, history |
|
|
|
|
|
with gr.Blocks(css="#chatbot{height:350px} .overflow-y-auto{height:500px}") as demo: |
|
chatbot = gr.Chatbot(elem_id="chatbot") |
|
state = gr.State([]) |
|
|
|
with gr.Row(): |
|
txt = gr.Textbox(show_label=False, placeholder="Enter text and press enter").style(container=False) |
|
|
|
txt.submit(predict, [txt, state], [chatbot, state]) |
|
|
|
demo.launch() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|