caidanfeng
commited on
Commit
·
1e02f46
1
Parent(s):
5120594
add
Browse files
app.py
CHANGED
|
@@ -64,132 +64,6 @@ class Conversation:
|
|
| 64 |
return message
|
| 65 |
|
| 66 |
|
| 67 |
-
# In[2]:
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
prompt = """你是一个中国厨师,用中文回答做菜的问题。你的回答需要满足以下要求:
|
| 71 |
-
1. 你的回答必须是中文
|
| 72 |
-
2. 回答限制在100个字以内"""
|
| 73 |
-
conv1 = Conversation(prompt, 3)
|
| 74 |
-
question1 = "你是谁?"
|
| 75 |
-
print("User : %s" % question1)
|
| 76 |
-
print("Assistant : %s\n" % conv1.ask(question1))
|
| 77 |
-
|
| 78 |
-
question2 = "请问鱼香肉丝怎么做?"
|
| 79 |
-
print("User : %s" % question2)
|
| 80 |
-
print("Assistant : %s\n" % conv1.ask(question2))
|
| 81 |
-
|
| 82 |
-
question3 = "那蚝油牛肉呢?"
|
| 83 |
-
print("User : %s" % question3)
|
| 84 |
-
print("Assistant : %s\n" % conv1.ask(question3))
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
# In[3]:
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
question4 = "我问你的第一个问题是什么?"
|
| 91 |
-
print("User : %s" % question4)
|
| 92 |
-
print("Assistant : %s\n" % conv1.ask(question4))
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
question5 = "我问你的第一个问题是什么?"
|
| 99 |
-
print("User : %s" % question5)
|
| 100 |
-
print("Assistant : %s\n" % conv1.ask(question5))
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
class Conversation2:
|
| 109 |
-
def __init__(self, prompt, num_of_round):
|
| 110 |
-
self.prompt = prompt
|
| 111 |
-
self.num_of_round = num_of_round
|
| 112 |
-
self.messages = []
|
| 113 |
-
self.messages.append({"role": "system", "content": self.prompt})
|
| 114 |
-
|
| 115 |
-
def ask(self, question):
|
| 116 |
-
try:
|
| 117 |
-
self.messages.append( {"role": "user", "content": question})
|
| 118 |
-
response = openai.ChatCompletion.create(
|
| 119 |
-
model="gpt-3.5-turbo",
|
| 120 |
-
messages=self.messages,
|
| 121 |
-
temperature=0.5,
|
| 122 |
-
max_tokens=2048,
|
| 123 |
-
top_p=1,
|
| 124 |
-
)
|
| 125 |
-
except Exception as e:
|
| 126 |
-
print(e)
|
| 127 |
-
return e
|
| 128 |
-
|
| 129 |
-
message = response["choices"][0]["message"]["content"]
|
| 130 |
-
num_of_tokens = response['usage']['total_tokens']
|
| 131 |
-
self.messages.append({"role": "assistant", "content": message})
|
| 132 |
-
|
| 133 |
-
if len(self.messages) > self.num_of_round*2 + 1:
|
| 134 |
-
del self.messages[1:3]
|
| 135 |
-
return message, num_of_tokens
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
# In[6]:
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
conv2 = Conversation2(prompt, 3)
|
| 142 |
-
questions = [question1, question2, question3, question4, question5]
|
| 143 |
-
for question in questions:
|
| 144 |
-
answer, num_of_tokens = conv2.ask(question)
|
| 145 |
-
print("询问 {%s} 消耗的token数量是 : %d" % (question, num_of_tokens))
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
import tiktoken
|
| 152 |
-
encoding = tiktoken.get_encoding("cl100k_base")
|
| 153 |
-
|
| 154 |
-
conv2 = Conversation2(prompt, 3)
|
| 155 |
-
question1 = "你是谁?"
|
| 156 |
-
answer1, num_of_tokens = conv2.ask(question1)
|
| 157 |
-
print("总共消耗的token数量是 : %d" % (num_of_tokens))
|
| 158 |
-
|
| 159 |
-
prompt_count = len(encoding.encode(prompt))
|
| 160 |
-
question1_count = len(encoding.encode(question1))
|
| 161 |
-
answer1_count = len(encoding.encode(answer1))
|
| 162 |
-
total_count = prompt_count + question1_count + answer1_count
|
| 163 |
-
print("Prompt消耗 %d Token, 问题消耗 %d Token,回答消耗 %d Token,总共消耗 %d Token" % (prompt_count, question1_count, answer1_count, total_count))
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
system_start_count = len(encoding.encode("<|im_start|>system\n"))
|
| 168 |
-
print(encoding.encode("<|im_start|>system\n"))
|
| 169 |
-
end_count = len(encoding.encode("<|im_end|>\n"))
|
| 170 |
-
print(encoding.encode("<|im_end|>\n"))
|
| 171 |
-
user_start_count = len(encoding.encode("<|im_start|>user\n"))
|
| 172 |
-
print(encoding.encode("<|im_start|>user\n"))
|
| 173 |
-
assistant_start_count = len(encoding.encode("<|im_start|>assistant\n"))
|
| 174 |
-
print(encoding.encode("<|im_start|>assistant\n"))
|
| 175 |
-
|
| 176 |
-
total_mark_count = system_start_count + user_start_count + assistant_start_count + end_count*2
|
| 177 |
-
print("系统拼接的标记消耗 %d Token" % total_mark_count)
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
get_ipython().run_line_magic('pip', 'install gradio')
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
# In[4]:
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
get_ipython().run_line_magic('pip', 'install --upgrade gradio')
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
# In[3]:
|
| 193 |
|
| 194 |
|
| 195 |
import gradio as gr
|
|
|
|
| 64 |
return message
|
| 65 |
|
| 66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
|
| 69 |
import gradio as gr
|