Created
May 23, 2024 15:33
-
-
Save janakiramm/0c000d1dff288860f212fb1c8d02c2b4 to your computer and use it in GitHub Desktop.
Function Calling with Open Hermes-2 Pro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from huggingface_hub import InferenceClient | |
client = InferenceClient("http://127.0.0.1:8080") | |
tools = [ | |
{ | |
"type": "function", | |
"function": { | |
"name": "get_flight_status", | |
"description": "Get status of a flight", | |
"parameters": { | |
"type": "object", | |
"properties": { | |
"flight": { | |
"type": "string", | |
"description": "Flight number" | |
} | |
}, | |
"required": ["flight"] | |
} | |
} | |
} | |
] | |
def chatbot(prompt): | |
messages = [ | |
{ | |
"role": "system", | |
"content": "You're a helpful assistant! Answer the users question best you can based on the tools provided. Be concise in your responses.", | |
}, | |
{ | |
"role": "user", | |
"content": prompt | |
}, | |
] | |
response = client.chat_completion(messages=messages, tools=tools) | |
tool_calls = response.choices[0].message.tool_calls | |
if tool_calls: | |
available_functions = { | |
"get_flight_status": get_flight_status, | |
} | |
for tool_call in tool_calls: | |
function_name = tool_call.function.name | |
function_to_call = available_functions[function_name] | |
function_args = tool_call.function.arguments | |
function_response = function_to_call(flight=function_args.get("flight")) | |
messages.append( | |
{ | |
"role": "tool", | |
"name": function_name, | |
"content": function_response | |
} | |
) | |
final_response = client.chat_completion(messages=messages) | |
return final_response | |
return response | |
res=chatbot("What's the status of EK226?") | |
print(res.choices[0].message.content) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment