glt3953 commited on
Commit
3ed7ffe
·
1 Parent(s): b72ae2a
Files changed (1) hide show
  1. app.py +7 -1
app.py CHANGED
@@ -1,9 +1,12 @@
1
  #安装一下 Python 的 Gradio 的包:conda install -c conda-forge gradio
 
2
  import gradio as gr
3
  import openai
4
  import os
 
5
 
6
  openai.api_key = os.environ.get("OPENAI_API_KEY")
 
7
 
8
  class Conversation:
9
  def __init__(self, prompt, num_of_round):
@@ -13,12 +16,15 @@ class Conversation:
13
  self.messages.append(self.prompt)
14
 
15
  def ask(self, question):
 
 
 
16
  try:
17
  self.messages.append(question)
18
  response = openai.Completion.create(
19
  engine="text-davinci-003",
20
  prompt=question,
21
- max_tokens=4096,
22
  n=1,
23
  stop=None,
24
  temperature=0.5,
 
1
  #安装一下 Python 的 Gradio 的包:conda install -c conda-forge gradio
2
+ #依赖tiktoken:pip install tiktoken,使用不同的 GPT 模型,对应着不同的 Tiktoken 的编码器模型:https://github.com/openai/openai-cookbook/blob/main/examples/How_to_count_tokens_with_tiktoken.ipynb。
3
  import gradio as gr
4
  import openai
5
  import os
6
+ import tiktoken
7
 
8
  openai.api_key = os.environ.get("OPENAI_API_KEY")
9
+ encoding = tiktoken.get_encoding("p50k_base")
10
 
11
  class Conversation:
12
  def __init__(self, prompt, num_of_round):
 
16
  self.messages.append(self.prompt)
17
 
18
  def ask(self, question):
19
+ if len(encoding.encode(prompt)) > 512:
20
+ return "输入的文字过长,请精简后再提问"
21
+
22
  try:
23
  self.messages.append(question)
24
  response = openai.Completion.create(
25
  engine="text-davinci-003",
26
  prompt=question,
27
+ max_tokens=3580,
28
  n=1,
29
  stop=None,
30
  temperature=0.5,