Given the following API response from pinecone (https://www.pinecone.io/docs/api/operation/query/)
results = {'matches': [{'id': 'yral5m',
'metadata': {'subreddit': '2qkq6',
'text': 'Black Friday SaaS Deals - 2022'},
'score': 0.772717535,
'sparseValues': {},
'values': []},
{'id': 'yqypa5',
'metadata': {'subreddit': '2qkq6',
'text': 'B2B sales interface.'},
'score': 0.74192214,
'sparseValues': {},
'values': []}],
'namespace': ''}
i am simply trying to return the JSON results from a function. (from a service to a controller) and getting a range of errors:
doing so with:
return results
yields RecursionError: maximum recursion depth exceeded in comparison
return json.dumps(results)
yeilds TypeError: Object of type QueryResponse is not JSON serializable
*QueryResponse is the type returned from pinecone
return jsonpickle.encode(query_results)
yeilds "null"
Very lost any advice would be appreciated!!
full code example:
Controller:
@router.post("/query/")
async def semantic_search(query: str, ):
result = await TopicQueryService.query(query)
return result
Service method:
@staticmethod
async def query(query) -> str:
index = PineConeService.get_or_create_index("openai")
embed = GPT3Service.get_embedding(query)
query_results = index.query(
vector=embed,
top_k=2,
include_metadata=True
)
return json.dumps(query_results)
Replacing Service Method query results with the logged response from index. query works fine eg below. Leading me to believe it is due to the QueryResponse object pinecone returns.
@staticmethod
async def query(query) -> str:
index = PineConeService.get_or_create_index("openai")
embed = GPT3Service.get_embedding(query)
logger.info(embed)
query_results = {'matches': [{'id': 'yral5m',
'metadata': {'subreddit': '2qkq6',
'text': 'Black Friday SaaS Deals - 2022'},
'score': 0.772717535,
'sparseValues': {},
'values': []},
{'id': 'yqypa5',
'metadata': {'subreddit': '2qkq6',
'text': 'B2B sales interface.'},
'score': 0.74192214,
'sparseValues': {},
'values': []}],
'namespace': ''}
return json.dumps(query_results)