|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding:utf-8 -*- |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import json |
| 6 | +import time |
| 7 | +from typing import Optional |
| 8 | + |
| 9 | +import requests |
| 10 | + |
| 11 | + |
| 12 | +class RAG(object): |
| 13 | + def __init__( |
| 14 | + self, |
| 15 | + project_id: str, |
| 16 | + api_key: str, |
| 17 | + ragapp_id: str, |
| 18 | + conversation_id: Optional[str] = None, |
| 19 | + ): |
| 20 | + self._project_id = project_id |
| 21 | + self._ragapp_id = ragapp_id |
| 22 | + self._api_key = api_key |
| 23 | + self._conversation_id = conversation_id |
| 24 | + self._rag_base_url = "https://rag.epsilla.com" |
| 25 | + self._headers = {"Content-Type": "application/json", "X-API-Key": self._api_key} |
| 26 | + self._timeout = 15 |
| 27 | + self._interval = 5 |
| 28 | + |
| 29 | + def start_new_conversation(self): |
| 30 | + if self._conversation_id is None: |
| 31 | + req_url = f"{self._rag_base_url}/conversation/{self._project_id}/{self._ragapp_id}/create" |
| 32 | + req_data = {"summary": "summary of the conversation"} |
| 33 | + resp = requests.post( |
| 34 | + req_url, |
| 35 | + data=json.dumps(req_data), |
| 36 | + headers=self._headers, |
| 37 | + timeout=self._timeout, |
| 38 | + verify=True, |
| 39 | + ) |
| 40 | + status_code = resp.status_code |
| 41 | + body = resp.json() |
| 42 | + resp.close() |
| 43 | + del resp |
| 44 | + |
| 45 | + if body["statusCode"] == 200: |
| 46 | + self._conversation_id = body["result"]["conversationId"] |
| 47 | + else: |
| 48 | + self._conversation_id = "" |
| 49 | + |
| 50 | + print(f"[INFO] current conversation id is {self._conversation_id}") |
| 51 | + |
| 52 | + def query(self, message: str): |
| 53 | + if self._conversation_id is None: |
| 54 | + self.start_new_conversation() |
| 55 | + |
| 56 | + answer = None |
| 57 | + contexts = None |
| 58 | + |
| 59 | + req_url = f"{self._rag_base_url}/chat/{self._project_id}/{self._ragapp_id}/{self._conversation_id}" |
| 60 | + req_data = {"message": message} |
| 61 | + resp = requests.post( |
| 62 | + req_url, |
| 63 | + data=json.dumps(req_data), |
| 64 | + headers=self._headers, |
| 65 | + timeout=self._timeout, |
| 66 | + verify=True, |
| 67 | + ) |
| 68 | + status_code = resp.status_code |
| 69 | + body = resp.json() |
| 70 | + resp.close() |
| 71 | + del resp |
| 72 | + |
| 73 | + if body["statusCode"] == 200: |
| 74 | + time.sleep(self._interval) |
| 75 | + req_message_id = body["result"] |
| 76 | + |
| 77 | + completed = False |
| 78 | + body = None |
| 79 | + while completed is not True: |
| 80 | + time.sleep(self._interval) |
| 81 | + req_url = f"{self._rag_base_url}/stream/{self._project_id}/{self._ragapp_id}/{req_message_id}" |
| 82 | + resp = requests.get( |
| 83 | + req_url, |
| 84 | + headers=self._headers, |
| 85 | + timeout=self._timeout, |
| 86 | + verify=True, |
| 87 | + ) |
| 88 | + status_code = resp.status_code |
| 89 | + body = resp.json() |
| 90 | + resp.close() |
| 91 | + del resp |
| 92 | + |
| 93 | + completed = body["result"]["completed"] |
| 94 | + if completed is True: |
| 95 | + break |
| 96 | + |
| 97 | + if body["statusCode"] == 200: |
| 98 | + answer = body["result"]["result"]["Generated Result"] |
| 99 | + contexts = [c["Content"] for c in body["result"]["result"]["knowledge"]] |
| 100 | + |
| 101 | + else: |
| 102 | + print("[ERROR] Failed to retrival answer&content from message") |
| 103 | + else: |
| 104 | + print("[ERROR] Failed to get response of the query message") |
| 105 | + |
| 106 | + return {"answer": answer, "contexts": contexts} |
| 107 | + |
| 108 | + |
| 109 | +if __name__ == "__main__": |
| 110 | + rag = RAG( |
| 111 | + project_id="**********", |
| 112 | + api_key="eps_**********", |
| 113 | + ragapp_id="**********", |
| 114 | + conversation_id="**********", |
| 115 | + ) |
| 116 | + |
| 117 | + rag.start_new_conversation() |
| 118 | + resp = rag.query("What's RAG?") |
| 119 | + |
| 120 | + print(f"[INFO] response is {resp}") |
0 commit comments