|
16 | 16 | from clueai.error import ClueaiError
|
17 | 17 | from clueai.generation import Generations, Generation, TokenLikelihood
|
18 | 18 | from clueai.match import Match, Matches, Score
|
| 19 | +from clueai.qa import QAs, QA, QAPair |
19 | 20 | from clueai.tokenize import Tokens
|
20 | 21 | from clueai.classify import Classifications, Classification, Example as ClassifyExample, Confidence
|
21 | 22 | from clueai.extract import Entity, Example as ExtractExample, Extraction, Extractions
|
@@ -317,6 +318,46 @@ def search(
|
317 | 318 |
|
318 | 319 | return Matches([match_instance])
|
319 | 320 |
|
| 321 | + def doc_qa_generate( |
| 322 | + self, |
| 323 | + doc: str, |
| 324 | + headers: dict = {}, |
| 325 | + model_name: str = None |
| 326 | + ): |
| 327 | + try: |
| 328 | + res = self.check_api_key() |
| 329 | + if not res['valid']: |
| 330 | + raise ClueaiError('invalid api key') |
| 331 | + except ClueaiError as e: |
| 332 | + raise ClueaiError( |
| 333 | + message=e.message, |
| 334 | + http_status=e.http_status, |
| 335 | + headers=e.headers) |
| 336 | + |
| 337 | + tmp_headers = { |
| 338 | + 'Api-Key': 'BEARER {}'.format(self.api_key), |
| 339 | + 'Content-Type': 'application/json', |
| 340 | + 'Request-Source': 'python-sdk', |
| 341 | + 'Model-name': model_name |
| 342 | + } |
| 343 | + if self.modelfun_version != '': |
| 344 | + tmp_headers['clueai-Version'] = self.modelfun_version |
| 345 | + tmp_headers.update(headers) |
| 346 | + response = requests.get(f"{self.clueai_api_url}/doc_auto_qa/?doc={doc}", |
| 347 | + headers=tmp_headers) |
| 348 | + response = response.json() |
| 349 | + if "result" not in response: |
| 350 | + raise ClueaiError(f'No result in response, please check or try. error{response}') |
| 351 | + |
| 352 | + result = response['result'] |
| 353 | + qa_pairs = [] |
| 354 | + for q, a in result['qa_pairs'].items(): |
| 355 | + qa_pairs.append(QAPair(q, a)) |
| 356 | + |
| 357 | + qa_instance = QA(doc, qa_pairs) |
| 358 | + |
| 359 | + return QAs([qa_instance]) |
| 360 | + |
320 | 361 | def text2image(
|
321 | 362 | self,
|
322 | 363 | prompt: str,
|
|
0 commit comments