From e4321a07c6ccbbe832319478c716128c2241d2eb Mon Sep 17 00:00:00 2001 From: ZiTao-Li <135263265+ZiTao-Li@users.noreply.github.com> Date: Wed, 6 Nov 2024 04:10:00 -0800 Subject: [PATCH] [HOTFIX] Change postAPI embedding model _parse_resonse to OpenAI format (#479) --- src/agentscope/models/post_model.py | 52 ++++++++++++++--------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/src/agentscope/models/post_model.py b/src/agentscope/models/post_model.py index def456c2f..626673c97 100644 --- a/src/agentscope/models/post_model.py +++ b/src/agentscope/models/post_model.py @@ -273,37 +273,37 @@ def _parse_response(self, response: dict) -> ModelResponse: Args: response (`dict`): The response obtained from the API. This parsing assume the - structure of the response is as following: + structure of the response is the same as OpenAI's as following: { - "code": 200, - "data": { - ... - "response": { - "data": [ - { - "embedding": [ - 0.001, - ... - ], - ... - } - ], - "model": "xxxx", - ... - }, - }, + "object": "list", + "data": [ + { + "object": "embedding", + "embedding": [ + 0.0023064255, + -0.009327292, + .... (1536 floats total for ada-002) + -0.0028842222, + ], + "index": 0 + } + ], + "model": "text-embedding-ada-002", + "usage": { + "prompt_tokens": 8, + "total_tokens": 8 + } } """ - if "data" not in response["data"]["response"]: - if "error" in response["data"]["response"]: - error_msg = response["data"]["response"]["error"]["message"] - else: - error_msg = response["data"]["response"] + if ( + "data" not in response + or len(response["data"]) < 1 + or "embedding" not in response["data"][0] + ): + error_msg = json.dumps(response, ensure_ascii=False, indent=2) logger.error(f"Error in embedding API call:\n{error_msg}") raise ValueError(f"Error in embedding API call:\n{error_msg}") - embeddings = [ - data["embedding"] for data in response["data"]["response"]["data"] - ] + embeddings = [data["embedding"] for data in response["data"]] return ModelResponse( embedding=embeddings, raw=response,