@@ -102,6 +102,35 @@ def _make_rag_request(
102
102
stream = stream ,
103
103
)
104
104
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
+
105
134
106
135
class RAGClient (BaseRAGClient ):
107
136
client : httpx .Client
@@ -120,7 +149,7 @@ def query_rag(
120
149
).to_httpx_request ()
121
150
)
122
151
resp .raise_for_status ()
123
- return resp . json ()[ "response" ]
152
+ return BaseRAGClient . _parse_rag_response ( resp )
124
153
125
154
def stream_rag (
126
155
self , message : str , context : typing .Optional [types .QueryContext ] = None
@@ -163,7 +192,7 @@ async def query_rag(
163
192
).to_httpx_request ()
164
193
)
165
194
resp .raise_for_status ()
166
- return resp . json ()[ "response" ]
195
+ return BaseRAGClient . _parse_rag_response ( resp )
167
196
168
197
async def stream_rag (
169
198
self , message : str , context : typing .Optional [types .QueryContext ] = None
0 commit comments