Spaces:
Build error
Build error
Commit ·
203f861
1
Parent(s): 006b3a2
feature: add api calling demo
Browse filesFiles changed (4)
2 unmodified lines34567891011121314151617181920212223242526274 unmodified lines32333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192932 unmodified lines969798991001011021031041051061071081091101111121131141151162 unmodified linesimport gradio as grfrom openai import OpenAIfrom huggingface_hub import InferenceClientfrom tenacity import retry, wait_random_exponential, stop_after_attemptOPENAI_KEY = os.getenv("OPENAI_KEY")client = OpenAI(api_key=OPENAI_KEY)def get_current_weather(location, unit="celsius"):"""Get the current weather in a given location"""if "taipei" in location.lower():return json.dumps({"location": "Taipei", "temperature": "10", "unit": unit})else:return json.dumps({"location": location, "temperature": "unknown"})@retry(wait=wait_random_exponential(multiplier=1, max=40), stop=stop_after_attempt(3))def chat_completion_request(messages, tools=None, tool_choice=None):print(f'query message {messages}')try:response = client.chat.completions.create(model='gpt-4o',messages=messages,tools=tools,tool_choice=tool_choice,4 unmodified linesprint("Unable to generate ChatCompletion response")print(f"Exception: {e}")return etools = [{"type": "function","function": {"name": "get_current_weather","description": "Get the current weather","parameters": {"type": "object","properties": {"location": {"type": "string","description": "The city and state, e.g. San Francisco, CA",},"unit": {"type": "string","enum": ["celsius", "fahrenheit"],"description": "The temperature unit to use. Infer this from the users location.",},},"required": ["location", "unit"],},}}]def respond(message,history: list[tuple[str, str]],):messages = [{"role": "system", "content": [{'type': 'text', 'text': 'You are a helpful agent'}]}]for val in history:if val[0]:messages.append({"role": "user", "content": [{'type': 'text', 'text': val[0]}]})if val[1]:messages.append({"role": "assistant", "content": [{'type': 'text', 'text': val[1]}]})messages.append({"role": "user", "content": [{'type': 'text', 'text': message}]})response = chat_completion_request(messages, tools=tools, tool_choice='auto')response_message = response.choices[0].messagetool_calls = response_message.tool_callsif tool_calls:available_functions = {"get_current_weather": get_current_weather,}messages.append(response_message)for tool_call in tool_calls:function_name = tool_call.function.namefunction_to_call = available_functions[function_name]function_args = json.loads(tool_call.function.arguments)function_response = function_to_call(location=function_args.get("location"),unit=function_args.get("unit"),)messages.append({2 unmodified lines"name": function_name,"content": function_response,})second_response = chat_completion_request(messages)messages.append({"role": "assistant", "content": [{'type': 'text', 'text': second_response.choices[0].message.content}]})return second_response.choices[0].message.contentmessages.append({"role": "assistant", "content": [{'type': 'text', 'text': response.choices[0].message.content}]})return response.choices[0].message.content"""For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface"""demo = gr.ChatInterface(respond,title='Function Calling Demo')if __name__ == "__main__":demo.launch()2 unmodified lines3456789101112131415161718192021224 unmodified lines2728293031323334353637383940414243444546474849505152535455565758596061626364656667686970712 unmodified lines74757677787980818283848586878889909192939495969798991001011021031042 unmodified linesimport gradio as grfrom openai import OpenAIfrom tenacity import retry, wait_random_exponential, stop_after_attemptfrom functions_definition import get_functions, get_openai_function_toolsOPENAI_KEY = os.getenv("OPENAI_KEY")client = OpenAI(api_key=OPENAI_KEY)@retry(wait=wait_random_exponential(multiplier=1, max=40), stop=stop_after_attempt(3))def chat_completion_request(messages, tools=None, tool_choice=None):print(f"query message {messages}")try:response = client.chat.completions.create(model="gpt-4o",messages=messages,tools=tools,tool_choice=tool_choice,4 unmodified linesprint("Unable to generate ChatCompletion response")print(f"Exception: {e}")return edef respond(message,history: list[tuple[str, str]],):messages = [{"role": "system","content": [{"type": "text", "text": "You are a helpful agent"}],}]for val in history:if val[0]:messages.append({"role": "user", "content": [{"type": "text", "text": val[0]}]})if val[1]:messages.append({"role": "assistant", "content": [{"type": "text", "text": val[1]}]})messages.append({"role": "user", "content": [{"type": "text", "text": message}]})response = chat_completion_request(messages, tools=get_openai_function_tools(), tool_choice="auto")response_message = response.choices[0].messagetool_calls = response_message.tool_callsif tool_calls:available_functions = get_functions()messages.append(response_message)for tool_call in tool_calls:function_name = tool_call.function.namefunction_to_call = available_functions[function_name]function_args = json.loads(tool_call.function.arguments)function_response = function_to_call(type=function_args.get("type"),)messages.append({2 unmodified lines"name": function_name,"content": function_response,})second_response = chat_completion_request(messages)messages.append({"role": "assistant","content": [{"type": "text", "text": second_response.choices[0].message.content}],})return second_response.choices[0].message.contentmessages.append({"role": "assistant","content": [{"type": "text", "text": response.choices[0].message.content}],})return response.choices[0].message.content"""For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface"""demo = gr.ChatInterface(respond, title="Function Calling Demo")if __name__ == "__main__":demo.launch()
123456789101112131415161718192021222324tools = [{"type": "function","function": {"name": "get_current_weather","description": "Get the current weather","parameters": {"type": "object","properties": {"location": {"type": "string","description": "The city and state, e.g. San Francisco, CA",},"unit": {"type": "string","enum": ["celsius", "fahrenheit"],"description": "The temperature unit to use. Infer this from the users location.",},},"required": ["location", "unit"],},}}]1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586import jsonimport requestsdef get_current_weather(location, unit="celsius"):"""Get the current weather in a given location"""if "taipei" in location.lower():return json.dumps({"location": "Taipei", "temperature": "10", "unit": unit})else:return json.dumps({"location": location, "temperature": "unknown"})def get_junyi_content(type):print(f"Fetching Junyi content of type {type}")base_url = "https://www.junyiacademy.org/api/v2/open/content/topicpage/"topic_id = "knsh-7a"url = f"{base_url}{topic_id}"response = requests.get(url)if response.status_code == 200:data = response.json()print(data)else:print(f"Request failed with status code {response.status_code}")if type == "english":return json.dumps({"content": "英文","url": "https://www.junyiacademy.org/junyi-english/eng-junior/eng-junior07/eng-junior07-knsh/knsh-7a",})def get_openai_function_tools():tools = [{"type": "function","function": {"name": "get_current_weather","description": "Get the current weather","parameters": {"type": "object","properties": {"location": {"type": "string","description": "The city and state, e.g. San Francisco, CA",},"unit": {"type": "string","enum": ["celsius", "fahrenheit"],"description": "The temperature unit to use. Infer this from the users location.",},},"required": ["location", "unit"],},},},{"type": "function","function": {"name": "get_junyi_content","description": "Get the content from Junyi Academy","parameters": {"type": "object","properties": {"type": {"type": "string","enum": ["english", "math"],"description": "The type of content to fetch",},},"required": ["type"],},},},]return toolsdef get_functions():return {"get_current_weather": get_current_weather,"get_junyi_content": get_junyi_content,}
__pycache__/app.cpython-311.pycAdded
__pycache__/functions_definition.cpython-311.pycAdded
app.py+40-52
functions_definition.py+81-19