Skip to content

Commit bb9d3d3

Browse files
committed
Update AI RAG response parsing.
1 parent e1e25c0 commit bb9d3d3

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

gel/ai/core.py

+31-2
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,35 @@ def _make_rag_request(
102102
stream=stream,
103103
)
104104

105+
@staticmethod
106+
def _parse_rag_response(
107+
resp: typing.Any
108+
)-> str:
109+
data: dict[str, typing.Any] = resp.json()
110+
111+
if text := data.get("text"):
112+
if isinstance(text, str):
113+
return text
114+
else:
115+
raise RuntimeError(
116+
f"Expected data.text to be a string, but got "
117+
f"{type(text).__name__}: {text}"
118+
)
119+
120+
elif response := data.get("response"):
121+
if isinstance(text, str):
122+
return text
123+
else:
124+
raise RuntimeError(
125+
f"Expected data.text to be a string, but got "
126+
f"{type(response).__name__}: {response}"
127+
)
128+
129+
raise RuntimeError(
130+
f"Expected response to include a non-empty string for either the "
131+
f"'text' or 'response' key, but got: {data}"
132+
)
133+
105134

106135
class RAGClient(BaseRAGClient):
107136
client: httpx.Client
@@ -120,7 +149,7 @@ def query_rag(
120149
).to_httpx_request()
121150
)
122151
resp.raise_for_status()
123-
return resp.json()["response"]
152+
return BaseRAGClient._parse_rag_response(resp)
124153

125154
def stream_rag(
126155
self, message: str, context: typing.Optional[types.QueryContext] = None
@@ -163,7 +192,7 @@ async def query_rag(
163192
).to_httpx_request()
164193
)
165194
resp.raise_for_status()
166-
return resp.json()["response"]
195+
return BaseRAGClient._parse_rag_response(resp)
167196

168197
async def stream_rag(
169198
self, message: str, context: typing.Optional[types.QueryContext] = None

0 commit comments

Comments
 (0)