Bytte commited on
Commit
beaf4f9
·
1 Parent(s): 4f0efa4

Upload 6 files

Browse files
Files changed (6) hide show
  1. Dockerfile +14 -0
  2. __init__.py +0 -0
  3. credentials.env +4 -0
  4. main.py +17 -0
  5. rag_retriever.py +60 -0
  6. requirements.txt +11 -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
+ pinecone_api_key = 'a63b52e8-4295-492e-9da6-645563cbcc13'
2
+ pinecome_environment = 'gcp-starter'
3
+ index_name = "rag-demo"
4
+ cohere_api_key = 'Ce6YE3fi4jSmCsKxp82qKh2ngjxyEfY7Ov3SVPbE'
main.py ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from rag_retriever import vector_search, cohere_completion_with_vector_search
2
+ from fastapi import FastAPI
3
+ from pydantic import BaseModel
4
+
5
+
6
+ class validation(BaseModel):
7
+ prompt: str
8
+
9
+
10
+ app = FastAPI()
11
+
12
+ @app.post('/rag')
13
+ async def retrival(item: validation):
14
+ rag = vector_search(item.prompt)
15
+ completion = cohere_completion_with_vector_search(item.prompt, rag)
16
+
17
+ return completion
rag_retriever.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pinecone
2
+ import cohere
3
+ from dotenv import dotenv_values
4
+
5
+
6
+ env_name = "credentials.env"
7
+ config = dotenv_values(env_name)
8
+
9
+ co = cohere.Client(config['cohere_api_key'])
10
+
11
+ index_name = config['index_name']
12
+
13
+ pinecone.init(
14
+ api_key=config['pinecone_api_key'],
15
+ environment=config['pinecone_environment']
16
+ )
17
+
18
+ index = pinecone.Index(index_name)
19
+
20
+ def vector_search(query):
21
+ rag_data = ""
22
+
23
+ xq = co.embed(
24
+ texts=[query],
25
+ model='embed-english-v3.0',
26
+ input_type='search_query'
27
+ )
28
+
29
+ res = index.query([xq.embeddings[0]], top_k=2, include_metadata=True)
30
+
31
+ for match in res['matches']:
32
+ if match['score'] < 0.8:
33
+ continue
34
+
35
+ rag_data += match['metadata']['text']
36
+
37
+ return rag_data
38
+
39
+
40
+ def cohere_completion_with_vector_search(prompt, rag):
41
+ DEFAULT_SYSTEM_PROMPT = '''You are a helpful, respectful and honest INTP-T AI Assistant named Gathnex AI. You are talking to a human User.
42
+ 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.
43
+ 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.
44
+ 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.
45
+ You can speak fluently in English.
46
+ Note: Sometimes the Context is not relevant to Question, so give Answer according to that sutiation.
47
+ '''
48
+
49
+ response = co.chat(
50
+ chat_history=[
51
+ {f"role": "system", "content": DEFAULT_SYSTEM_PROMPT},
52
+ {f"role": "user", "content": rag},
53
+ ],
54
+ message=prompt
55
+ )
56
+
57
+ return response.text
58
+
59
+
60
+
requirements.txt ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ pinecone-client
2
+ openai
3
+ datasets
4
+ cohere
5
+ pandas
6
+ google-colab
7
+ python-multipart
8
+ fastapi
9
+ pydantic
10
+ uvicorn
11
+ python-dotenv