jzhang533 commited on
Commit
8cfb246
·
1 Parent(s): afcda5c

support streaming

Browse files

Signed-off-by: Zhang Jun <[email protected]>

Files changed (1) hide show
  1. app.py +19 -15
app.py CHANGED
@@ -4,20 +4,17 @@ from openai import OpenAI
4
 
5
 
6
  title = "ERNIE 4.5 : BAIDU's LLM"
7
- description = '''
8
  - Official Website: <https://yiyan.baidu.com/> (UI in Chinese)
9
  - Twitter post: [We've just unveiled ERNIE 4.5 & X1! 🚀](https://x.com/Baidu_Inc/status/1901089355890036897)
10
  - API services: [Qianfan Large Model Platform](https://cloud.baidu.com/product-s/qianfan_home) (cloud platform providing LLM services, UI in Chinese)
11
- '''
12
 
13
 
14
  qianfan_api_key = os.getenv("QIANFAN_TOKEN")
15
  qianfan_model = "ernie-4.5-8k-preview"
16
 
17
- client = OpenAI(
18
- base_url='https://qianfan.baidubce.com/v2',
19
- api_key=qianfan_api_key
20
- )
21
 
22
 
23
  def respond(
@@ -39,13 +36,20 @@ def respond(
39
  messages.append({"role": "user", "content": message})
40
 
41
  response = client.chat.completions.create(
42
- model=qianfan_model,
43
- messages=messages,
44
- max_completion_tokens = max_tokens,
45
- temperature = temperature,
46
- top_p = top_p
47
- )
48
- return response.choices[0].message.content
 
 
 
 
 
 
 
49
 
50
  demo = gr.ChatInterface(
51
  respond,
@@ -61,8 +65,8 @@ demo = gr.ChatInterface(
61
  label="Top-p (nucleus sampling)",
62
  ),
63
  ],
64
- title = title,
65
- description = description,
66
  )
67
 
68
  if __name__ == "__main__":
 
4
 
5
 
6
  title = "ERNIE 4.5 : BAIDU's LLM"
7
+ description = """
8
  - Official Website: <https://yiyan.baidu.com/> (UI in Chinese)
9
  - Twitter post: [We've just unveiled ERNIE 4.5 & X1! 🚀](https://x.com/Baidu_Inc/status/1901089355890036897)
10
  - API services: [Qianfan Large Model Platform](https://cloud.baidu.com/product-s/qianfan_home) (cloud platform providing LLM services, UI in Chinese)
11
+ """
12
 
13
 
14
  qianfan_api_key = os.getenv("QIANFAN_TOKEN")
15
  qianfan_model = "ernie-4.5-8k-preview"
16
 
17
+ client = OpenAI(base_url="https://qianfan.baidubce.com/v2", api_key=qianfan_api_key)
 
 
 
18
 
19
 
20
  def respond(
 
36
  messages.append({"role": "user", "content": message})
37
 
38
  response = client.chat.completions.create(
39
+ model=qianfan_model,
40
+ messages=messages,
41
+ max_completion_tokens=max_tokens,
42
+ temperature=temperature,
43
+ top_p=top_p,
44
+ stream=True,
45
+ )
46
+
47
+ output_message = ""
48
+ for chunk in response:
49
+ token = chunk.choices[0].delta.content
50
+ output_message += token
51
+ yield output_message
52
+
53
 
54
  demo = gr.ChatInterface(
55
  respond,
 
65
  label="Top-p (nucleus sampling)",
66
  ),
67
  ],
68
+ title=title,
69
+ description=description,
70
  )
71
 
72
  if __name__ == "__main__":