Created
February 21, 2024 12:44
-
-
Save tommymalmqvist/d3402b11c47b4b5765cf0c4fc7e2b608 to your computer and use it in GitHub Desktop.
ollama function calling
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
import json | |
import requests | |
from langchain_community.llms.ollama import Ollama | |
from langchain_core.utils.function_calling import convert_to_openai_tool | |
from langchain_experimental.llms.ollama_functions import OllamaFunctions | |
def get_invoice_data(start_date: str, end_date: str): | |
"""Get the total amount invoiced between two dates. | |
The format of the two dates must be in either 'YYYY-MM-DD' or 'YYYY-MM'. | |
You can not combine the two different formats in the same request. | |
Either start date and end date are both 'YYYY-MM-DD' or both 'YYYY-MM'. | |
Args: | |
start_date: Start date, a string in the format 'YYYY-MM-DD' or 'YYYY-MM' | |
end_date: End date, a string in the format 'YYYY-MM-DD' or 'YYYY-MM' | |
""" | |
response = requests.get( | |
f"http://0.0.0.0:8000/api/public/invoices/invoices?start_date={start_date}&end_date={end_date}") | |
total_amount = response.json()["total_amount"] | |
return total_amount | |
def main(): | |
# Convert the function definition to the schema expected by OpenAI tools | |
schema = convert_to_openai_tool(get_invoice_data) # Could be used to generate the get_invoice_data_schema | |
get_invoice_data_schema = \ | |
{ | |
'name': 'get_invoice_data', | |
'description': "Get the total amount invoiced between two dates. The format of the two dates must be in either 'YYYY-MM-DD' or 'YYYY-MM'. You can not combine the two different formats in the same request. Either start date and end date are both 'YYYY-MM-DD' or both 'YYYY-MM'.", | |
'parameters': { | |
'type': 'object', | |
'properties': { | |
'start_date': { | |
'type': 'string', | |
'description': "Start date, a string in the format 'YYYY-MM-DD' or 'YYYY-MM'" | |
}, | |
'end_date': { | |
'type': 'string', | |
'description': "End date, a string in the format 'YYYY-MM-DD' or 'YYYY-MM'" | |
} | |
}, | |
'required': ['start_date', 'end_date'] | |
} | |
} | |
model = OllamaFunctions(model="llama2:13b") | |
model = model.bind( | |
functions=[ | |
get_invoice_data_schema # Directly pass the dictionary here, not a JSON string | |
], | |
function_call={ | |
"name": "get_invoice_data", | |
"description": "Get the total amount invoiced between two dates.", | |
"arguments": [ | |
{ | |
"name": "start_date", | |
"type": "string", | |
"description": "Start date, a string in the format 'YYYY-MM-DD' or 'YYYY-MM'" | |
}, | |
{ | |
"name": "end_date", | |
"type": "string", | |
"description": "End date, a string in the format 'YYYY-MM-DD' or 'YYYY-MM'" | |
} | |
] | |
} | |
) | |
response = model.invoke("what is the invoiced amount between 2023-03-01 and 2023-04-01?") | |
function = response.additional_kwargs['function_call']['name'] | |
args = json.loads(response.additional_kwargs['function_call']['arguments']) | |
if function == "get_invoice_data": | |
start_date = args["start_date"] | |
end_date = args["end_date"] | |
result = get_invoice_data(start_date, end_date) | |
llm = Ollama(model="llama2:13b") | |
cont = llm.invoke( | |
f"You are a financial adviser helping me with the reporting of company finances. I'll send you information and you will answer me. My message is: The total amount invoiced between {start_date} and {end_date} was {result} SEK for the company, what do you think about that?") | |
print(cont) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment