Last active
July 10, 2023 15:02
-
-
Save Shaunwei/399b63aa155950cb2191b33b09fc0899 to your computer and use it in GitHub Desktop.
Microsoft Guidance OpenAI function calling template
This file contains hidden or 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 openai_function_call import openai_function | |
import guidance | |
@openai_function | |
def get_weather(location: str, unit: str = 'fahrenheit', date: str = 'today'): | |
""" Get the current weather in a given location and date.""" | |
weather_info = { | |
'location': location, | |
'unit': unit, | |
'temperature': '60' if unit == 'fahrenheit' else '15', | |
'date': date, | |
} | |
return weather_info | |
@openai_function | |
def get_today_date(date: str) -> str: | |
""" Get today's date.""" | |
return '2023-06-24' | |
await_program = guidance(""" | |
{{~#system~}} | |
You are a helpful assistant. You don't know what time it is. | |
{{>tool_def functions=functions}} | |
{{~/system~}} | |
{{~#user~}} | |
{{ set 'query' (await 'query') }} | |
{{~/user~}} | |
{{~#each range(10)~}} | |
{{~#assistant~}} | |
{{gen 'answer' temperature=0 max_tokens=50 function_call="auto"}} | |
{{~/assistant~}} | |
{{set 'function_call' extract_function_call(answer)}} | |
{{~#if not function_call}}{{break}}{{/if~}} | |
{{set 'answer' await('call_result')}} | |
{{~#function name=function_call.__name__~}} | |
{{answer}} | |
{{~/function~}} | |
{{~/each~}}""") | |
# call the program, passing in the function definition we want to use as JSON | |
executed_await_program = await_program( | |
functions=[ | |
get_weather.openai_schema, | |
get_today_date.openai_schema, | |
], | |
get_weather=get_weather, | |
get_today_date=get_today_date, | |
) | |
# Change to this for terminal input -> executed_await_program = executed_await_program(query=input()) | |
executed_await_program = executed_await_program(query="Get today's weather in San Francisco") | |
def run_call(program): | |
call = program["function_call"] | |
if call.__name__ == "get_today_date": | |
result = get_today_date(**call.__kwdefaults__) | |
elif call.__name__ == "get_weather": | |
result = get_weather(**call.__kwdefaults__) | |
return program(call_result=result) | |
run_call(executed_await_program) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment