Spaces:
Running
Running
Initial commit
Browse files- Dockerfile +14 -0
- __init__.py +0 -0
- credentials.env +4 -0
- main.py +17 -0
- rag_retriver.py +49 -0
- requirements.txt +7 -0
Dockerfile
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.9
|
2 |
+
|
3 |
+
WORKDIR /code
|
4 |
+
|
5 |
+
COPY ./requirements.txt /code/requirements.txt
|
6 |
+
|
7 |
+
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
8 |
+
|
9 |
+
COPY ./__init__.py /code/__init__.py
|
10 |
+
COPY ./credentials.env /code/credentials.env
|
11 |
+
COPY ./rag_retriver.py /code/rag_retriver.py
|
12 |
+
COPY ./main.py /code/main.py
|
13 |
+
|
14 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
__init__.py
ADDED
File without changes
|
credentials.env
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
openai_api = "openai api key"
|
2 |
+
Pinecone_api_key = "Pinecone api key"
|
3 |
+
Pinecone_environment = "gcp-starter"
|
4 |
+
index_name = "index name"
|
main.py
ADDED
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from rag_retriver import Vector_search, GPT_completion_with_vector_search
|
2 |
+
from fastapi import FastAPI
|
3 |
+
from pydantic import BaseModel
|
4 |
+
|
5 |
+
#Pydantic object
|
6 |
+
class validation(BaseModel):
|
7 |
+
prompt: str
|
8 |
+
|
9 |
+
#Fast API
|
10 |
+
app = FastAPI()
|
11 |
+
|
12 |
+
|
13 |
+
@app.post("/Gathnex_Rag_System")
|
14 |
+
async def retrival_augmented_generation(item: validation):
|
15 |
+
rag = Vector_search(item.prompt)
|
16 |
+
completion = GPT_completion_with_vector_search(item.prompt, rag)
|
17 |
+
return completion
|
rag_retriver.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import pinecone
|
3 |
+
from openai import OpenAI
|
4 |
+
from dotenv import dotenv_values
|
5 |
+
|
6 |
+
#Loading Credentials
|
7 |
+
env_name = "credentials.env"
|
8 |
+
config = dotenv_values(env_name)
|
9 |
+
client = OpenAI(api_key= config["openai_api"])
|
10 |
+
|
11 |
+
|
12 |
+
#Connection
|
13 |
+
index_name = config["index_name"]
|
14 |
+
# initialize connection to pinecone (get API key at app.pinecone.io)
|
15 |
+
pinecone.init(
|
16 |
+
api_key = config["Pinecone_api_key"],
|
17 |
+
environment = config["Pinecone_environment"]
|
18 |
+
)
|
19 |
+
index = pinecone.Index(index_name)
|
20 |
+
|
21 |
+
#Vector Search
|
22 |
+
def Vector_search(query):
|
23 |
+
Rag_data = ""
|
24 |
+
xq = client.embeddings.create(input=query,model="text-embedding-ada-002")
|
25 |
+
res = index.query([xq.data[0].embedding], top_k=2, include_metadata=True)
|
26 |
+
for match in res['matches']:
|
27 |
+
if match['score'] < 0.80:
|
28 |
+
continue
|
29 |
+
Rag_data += match['metadata']['text']
|
30 |
+
return Rag_data
|
31 |
+
|
32 |
+
#GPT Completion
|
33 |
+
def GPT_completion_with_vector_search(prompt, rag):
|
34 |
+
DEFAULT_SYSTEM_PROMPT = '''You are a helpful, respectful and honest INTP-T AI Assistant named Gathnex AI. You are talking to a human User.
|
35 |
+
Always answer as helpfully and logically as possible, while being safe. Your answers should not include any harmful, political, religious, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature.
|
36 |
+
If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information.
|
37 |
+
You also have access to RAG vectore database access which has Indian Law data. Be careful when giving response, sometime irrelevent Rag content will be there so give response effectivly to user based on the prompt.
|
38 |
+
You can speak fluently in English.
|
39 |
+
Note: Sometimes the Context is not relevant to Question, so give Answer according to that based on sutiation.
|
40 |
+
'''
|
41 |
+
response = client.chat.completions.create(
|
42 |
+
model="gpt-3.5-turbo-1106",
|
43 |
+
messages=[
|
44 |
+
{f"role": "system", "content": DEFAULT_SYSTEM_PROMPT},
|
45 |
+
{f"role": "user", "content": rag +", Prompt: "+ prompt},
|
46 |
+
]
|
47 |
+
)
|
48 |
+
|
49 |
+
return response.choices[0].message.content
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
openai
|
2 |
+
python-multipart
|
3 |
+
fastapi
|
4 |
+
pydantic
|
5 |
+
uvicorn
|
6 |
+
python-dotenv
|
7 |
+
pinecone-client
|