Skip to content

Commit 68c7094

Browse files
committed
update pyepsilla
1 parent 7ae5204 commit 68c7094

13 files changed

Lines changed: 384 additions & 64 deletions

File tree

examples/hello_epsilla.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
# client = vectordb.Client(protocol='https', host='demo.epsilla.com', port='443')
1717

1818
# Load DB with path
19-
## pay attention to change db_path to persistent volume for production environment
19+
# pay attention to change db_path to persistent volume for production environment
2020
status_code, response = client.load_db(db_name="MyDB", db_path="/data/epsilla_demo")
2121
print(response)
2222

examples/hello_epsilla_rag.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env python
2+
# -*- coding:utf-8 -*-
3+
4+
# Try this simple example for epsilla rag
5+
# 1. create client to connect to epsilla rag
6+
# 2. launch a new converstation with epsilla rag
7+
# 3. send a query and wait the response from epsilla ragg
8+
9+
import sys
10+
11+
from pyepsilla import cloud
12+
13+
# Connect to Epsilla RAG
14+
client = cloud.RAG(
15+
project_id="**********",
16+
api_key="eps_**********",
17+
ragapp_id="**********",
18+
conversation_id="**********",
19+
)
20+
21+
# Start a new conversation with RAG
22+
client.start_new_conversation()
23+
resp = client.query("What's RAG?")
24+
25+
print(f"[INFO] response is {resp}")

poetry.lock

Lines changed: 114 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyepsilla/cloud/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#!/usr/bin/env python
22
# -*- coding:utf-8 -*-
33

4+
from ..utils.rag import RAG
45
from .client import Client
56
from .sentry import init_sentry
67

7-
init_sentry()
8+
init_sentry()

pyepsilla/cloud/client.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22
# -*- coding:utf-8 -*-
33
from __future__ import annotations
44

5-
import datetime
65
import json
7-
import pprint
8-
import socket
96
from typing import Optional, Union
107

118
import requests
@@ -21,9 +18,7 @@ class Client(object):
2118
def __init__(self, project_id: str, api_key: str, headers: dict = None):
2219
self._project_id = project_id
2320
self._apikey = api_key
24-
self._baseurl = "https://dispatch.epsilla.com/api/v3/project/{}".format(
25-
self._project_id
26-
)
21+
self._baseurl = f"https://dispatch.epsilla.com/api/v3/project/{self._project_id}" # type: ignore
2722
self._timeout = 10
2823
self._header = {
2924
"Content-type": "application/json",
@@ -117,9 +112,7 @@ def __init__(
117112
self._db_id = db_id
118113
self._api_key = api_key
119114
self._public_endpoint = public_endpoint
120-
self._baseurl = "https://{}/api/v3/project/{}/vectordb/{}".format(
121-
self._public_endpoint, self._project_id, self._db_id
122-
)
115+
self._baseurl = f"https://{self._public_endpoint}/api/v3/project/{self._project_id}/vectordb/{self._db_id}"
123116
self._header = {"Content-type": "application/json", "X-API-Key": self._api_key}
124117
if headers is not None:
125118
self._header.update(headers)

pyepsilla/cloud/sentry.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
from pyepsilla.vectordb.version import __version__
1717
from sentry_sdk.integrations.atexit import AtexitIntegration
1818

19-
CONFIG_URL = "https://config.epsilla.com/candidate.json"
20-
SENTRY_DSN = "https://3f89b94a4a2e7620c8ecce81cb302d43@o4507288359862272.ingest.us.sentry.io/4507288364908545"
19+
CONFIG_URL = "https://config.epsilla.com/client.json"
20+
SENTRY_DSN = "https://c48db3ca1d2af76b6ef5c6dd8a3efac3@o4507871625347072.ingest.us.sentry.io/4507871626985472"
2121

2222
try:
2323
r = requests.get(CONFIG_URL, headers={"Agent": "PyEpsilla Cloud Client"}, timeout=2)

pyepsilla/enterprise/client.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22
# -*- coding:utf-8 -*-
33
from __future__ import annotations
44

5-
import datetime
65
import json
7-
import pprint
8-
import socket
96
from typing import Optional, Union
107

118
import requests

pyepsilla/enterprise/sentry.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
from pyepsilla.vectordb.version import __version__
1717
from sentry_sdk.integrations.atexit import AtexitIntegration
1818

19-
CONFIG_URL = "https://config.epsilla.com/candidate.json"
20-
SENTRY_DSN = "https://3f89b94a4a2e7620c8ecce81cb302d43@o4507288359862272.ingest.us.sentry.io/4507288364908545"
19+
CONFIG_URL = "https://config.epsilla.com/client.json"
20+
SENTRY_DSN = "https://c48db3ca1d2af76b6ef5c6dd8a3efac3@o4507871625347072.ingest.us.sentry.io/4507871626985472"
2121

2222
try:
2323
r = requests.get(

pyepsilla/utils/rag.py

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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

Comments
 (0)