mirror of
https://github.com/ollama/ollama-python.git
synced 2026-07-21 09:05:23 -04:00
[PR #199] [CLOSED] fix: Ensure JSON response is properly parsed in generate function when format is set to 'json' #407
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/ollama/ollama-python/pull/199
Author: @wyy511511
Created: 6/28/2024
Status: ❌ Closed
Base:
main← Head:main📝 Commits (1)
eb0077cfix: Ensure JSON response is properly parsed in generate function when format is set to 'json'📊 Changes
1 file changed (+8 additions, -1 deletions)
View changed files
📝
ollama/_client.py(+8 -1)📄 Description
When I set
formatas"json", my response looks like this:{'model': 'llama3', 'created_at': '2024-06-28T05:44:32.12715Z', 'response': '{"results": [\n{\n"entity_type": "IMEI",\n"text": "06-184755-866851-3"\n}\n]}\n\n \n\n ', 'done': True, 'done_reason': 'stop', 'context': [], 'total_duration': 4528553708, 'load_duration': 2316500, 'prompt_eval_count': 437, 'prompt_eval_duration': 2743245000, 'eval_count': 32, 'eval_duration': 1781380000}As you can notice, although it indeed returns a JSON object, the previous code parses the
'response'as a string (hence the presence of\ncharacters and type checking result).After I added some code to correctly parse the JSON response, the response now looks like this:
{'model': 'llama3', 'created_at': '2024-06-28T09:16:56.417739Z', 'response': {'results': [{'entity_type': 'IMEI', 'text': '06-184755-866851-3'}]}, 'done': True, 'done_reason': 'stop', 'context':It becomes cleaner and matches my expected format.
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.