ChenyuRabbitLove commited on
Commit
7b67ae6
1 Parent(s): 67a2fe5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -20
app.py CHANGED
@@ -1,19 +1,63 @@
1
  import gradio as gr
 
2
  from huggingface_hub import InferenceClient
 
3
 
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
 
10
  def respond(
11
  message,
12
  history: list[tuple[str, str]],
13
  system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
  ):
18
  messages = [{"role": "system", "content": system_message}]
19
 
@@ -25,19 +69,34 @@ def respond(
25
 
26
  messages.append({"role": "user", "content": message})
27
 
28
- response = ""
29
-
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
 
39
- response += token
40
- yield response
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
 
42
  """
43
  For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
@@ -45,7 +104,7 @@ For information on how to customize the ChatInterface, peruse the gradio docs: h
45
  demo = gr.ChatInterface(
46
  respond,
47
  additional_inputs=[
48
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
49
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
50
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
51
  gr.Slider(
 
1
  import gradio as gr
2
+ from openai import OpenAI
3
  from huggingface_hub import InferenceClient
4
+ from tenacity import retry, wait_random_exponential, stop_after_attempt
5
 
6
+ OPENAI_KEY = os.getenv("OPENAI_KEY")
7
+ client = OpenAI(api_key=OPEN_AI_KEY)
8
+
9
+ def get_current_weather(location, unit="celsius"):
10
+ """Get the current weather in a given location"""
11
+ if "taipei" in location.lower():
12
+ return json.dumps({"location": "Taipei", "temperature": "10", "unit": unit})
13
+ else:
14
+ return json.dumps({"location": location, "temperature": "unknown"})
15
+
16
+ @retry(wait=wait_random_exponential(multiplier=1, max=40), stop=stop_after_attempt(3))
17
+ def chat_completion_request(messages, tools=None, tool_choice=None, model=GPT_MODEL):
18
+ try:
19
+ response = client.chat.completions.create(
20
+ model=model,
21
+ messages=messages,
22
+ tools=tools,
23
+ tool_choice=tool_choice,
24
+ )
25
+ return response
26
+ except Exception as e:
27
+ print("Unable to generate ChatCompletion response")
28
+ print(f"Exception: {e}")
29
+ return e
30
+
31
+ tools = [
32
+ {
33
+ "type": "function",
34
+ "function": {
35
+ "name": "get_current_weather",
36
+ "description": "Get the current weather",
37
+ "parameters": {
38
+ "type": "object",
39
+ "properties": {
40
+ "location": {
41
+ "type": "string",
42
+ "description": "The city and state, e.g. San Francisco, CA",
43
+ },
44
+ "unit": {
45
+ "type": "string",
46
+ "enum": ["celsius", "fahrenheit"],
47
+ "description": "The temperature unit to use. Infer this from the users location.",
48
+ },
49
+ },
50
+ "required": ["location", "unit"],
51
+ },
52
+ }
53
+ }
54
+ ]
55
 
56
 
57
  def respond(
58
  message,
59
  history: list[tuple[str, str]],
60
  system_message,
 
 
 
61
  ):
62
  messages = [{"role": "system", "content": system_message}]
63
 
 
69
 
70
  messages.append({"role": "user", "content": message})
71
 
72
+ response = chat_completion_request(messages, tools=tools, tool_choice='auto')
 
 
 
 
 
 
 
 
 
73
 
74
+ response_message = response.choices[0].message
75
+ tool_calls = response_message.tool_calls
76
+ if tool_calls:
77
+ available_functions = {
78
+ "get_current_weather": get_current_weather,
79
+ }
80
+ messages.append(response_message)
81
+ for tool_call in tool_calls:
82
+ function_name = tool_call.function.name
83
+ function_to_call = available_functions[function_name]
84
+ function_args = json.loads(tool_call.function.arguments)
85
+ function_response = function_to_call(
86
+ location=function_args.get("location"),
87
+ unit=function_args.get("unit"),
88
+ )
89
+ messages.append(
90
+ {
91
+ "tool_call_id": tool_call.id,
92
+ "role": "tool",
93
+ "name": function_name,
94
+ "content": function_response,
95
+ }
96
+ )
97
+ second_response = chat_completion_request(messages)
98
+ print(second_response)
99
+ return second_response
100
 
101
  """
102
  For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
 
104
  demo = gr.ChatInterface(
105
  respond,
106
  additional_inputs=[
107
+ gr.Textbox(value="", label="System message"),
108
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
109
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
110
  gr.Slider(