You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.4 KiB
Python

"""
This file implements the parsing strategy for a response to an LLM. This has been tested on :
- ollama gemma4
- azure openai gpt-6-astra
"""
import json
import pandas as pd
def format(_data) :
if type(_data) == str :
_data = json.loads(_data)
return pd.DataFrame(_data).fillna(0).to_dict(orient='records')
def gemma4_12b(_stream) :
try:
_data = json.loads(_stream.replace('```json','').replace('```','').strip())
_data['data'] = format(_data['data'])
if 'analysis' in _data:
for _item in _data['analysis'] :
if 'data' in _item :
_item['data'] = format(_item['data'])
#
#
return _data
except Exception as e:
# print ([' Error ',e])
pass
return _stream
def gpt6_astra(_stream):
try:
_data = _stream[0]['text'] if _stream[0]['type'] == 'text' else _stream[1]['text']
if type(_data) == str :
_data = json.loads(_data)
_data['data'] = format(_data['data'])
if 'analysis' in _data:
for _item in _data['analysis'] :
if 'data' in _item :
_item['data'] = format(_item['data'])
#
#
return _data
except Exception as e:
print ([' Error ',e])
pass
return _stream