Ollama now allows for responses to be formatted to collect specific data. You feed it a format and it will return data values in that format.
The example that Ollama provides uses the Pydantic Module. The JSON return is turned into a class, and then you can access the values in the class by using class_name.value .
This seems to work well if you have a specific type of data to be interpreted and can write a good prompt.
This example allows you to input a value as if you were leaving a message for someone, and then it pulls out the name, phone number and email address of the person leaving the message.
Install Ollama: https://ollama.com/download
python3 -m pip install ollama
python3 -m pip install pydantic
from ollama import chat
from pydantic import BaseModel
class Data(BaseModel):
name: str
phone: str
email: str
query = 'what is the name,phone number and email address of the person leaving the message.'
data = input('Message: ')
response = chat(
messages=[
{
'role': 'user',
'content': f'{query} -- {data}',
}
],
model='llama3.1',
format=Data.model_json_schema(),
)
print(response)
data = Data.model_validate_json(response['message']['content'])
print(type(data))
print(data.name)
print(data.phone)
print(data.email)
Message: yo bob dis be james, hit me back at 4445556666
{'model': 'llama3.1', 'created_at': '2024-12-22T20:46:27.994314Z', 'message': {'role': 'assistant', 'content': '{ "name": "James", "phone": "444-555-6666", "email": "not provided" }'}, 'done_reason': 'stop', 'done': True, 'total_duration': 750765375, 'load_duration': 42528542, 'prompt_eval_count': 43, 'prompt_eval_duration': 226000000, 'eval_count': 27, 'eval_duration': 481000000}
<class '__main__.Data'>
James
444-555-6666
not provided
Message: hello sir please contact me back at tim@aol.com or 4445556666, this is Tim from the front office
{'model': 'llama3.1', 'created_at': '2024-12-22T20:53:00.571232Z', 'message': {'role': 'assistant', 'content': '{ "name": "Tim", "phone": "444-555-6666", "email": "tim@aol.com" }'}, 'done_reason': 'stop', 'done': True, 'total_duration': 1351081334, 'load_duration': 578890834, 'prompt_eval_count': 53, 'prompt_eval_duration': 244000000, 'eval_count': 29, 'eval_duration': 526000000}
<class '__main__.Data'>
Tim
444-555-6666
tim@aol.com
Comments