Rag is ready?
Browse files- App/Chat/utils/RAG.py +58 -25
App/Chat/utils/RAG.py
CHANGED
@@ -1,32 +1,65 @@
|
|
1 |
import aiohttp
|
2 |
-
import asyncio
|
|
|
|
|
3 |
import google.generativeai as palm
|
4 |
-
from
|
5 |
-
|
6 |
-
|
7 |
-
from langchain import PromptTemplate
|
8 |
-
import os
|
9 |
-
PALM_API = ''
|
10 |
-
API_KEY=os.environ.get("PALM_API",PALM_API)
|
11 |
palm.configure(api_key=API_KEY)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
-
|
15 |
-
|
16 |
-
llm = GooglePalm(
|
17 |
-
google_api_key=API_KEY, **{ "safety_settings": [
|
18 |
-
{"category": "HARM_CATEGORY_DEROGATORY", "threshold": 4},
|
19 |
-
{"category": "HARM_CATEGORY_TOXICITY", "threshold": 4},
|
20 |
-
{"category": "HARM_CATEGORY_VIOLENCE", "threshold": 4},
|
21 |
-
{"category": "HARM_CATEGORY_SEXUAL", "threshold": 4},
|
22 |
-
{"category": "HARM_CATEGORY_MEDICAL", "threshold": 4},
|
23 |
-
{"category": "HARM_CATEGORY_DANGEROUS", "threshold": 4},
|
24 |
-
]})
|
25 |
-
text_splitter = RecursiveCharacterTextSplitter(separators=["\n\n", "\n","."], chunk_size=40_000, chunk_overlap=500)
|
26 |
-
with open('./sample.txt', 'r') as file:
|
27 |
-
essay = file.read()
|
28 |
|
29 |
-
|
30 |
-
|
31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
|
|
1 |
import aiohttp
|
2 |
+
import asyncio
|
3 |
+
import json,os
|
4 |
+
import yaml
|
5 |
import google.generativeai as palm
|
6 |
+
from App.Embedding.utils.Initialize import search
|
7 |
+
PALM_API = ""
|
8 |
+
API_KEY = os.environ.get("PALM_API", PALM_API)
|
|
|
|
|
|
|
|
|
9 |
palm.configure(api_key=API_KEY)
|
10 |
+
class GenerativeAIAssistant:
|
11 |
+
def __init__(self, api_key=API_KEY, model='chat-bison-001', temperature=0.85,
|
12 |
+
candidate_count=1, top_k=40, top_p=0.95):
|
13 |
+
self.api_key = api_key
|
14 |
+
self.model = model
|
15 |
+
self.temperature = temperature
|
16 |
+
self.candidate_count = candidate_count
|
17 |
+
self.top_k = top_k
|
18 |
+
self.top_p = top_p
|
19 |
+
self.examples=[{"input": {"content": "hello"}, "output": {"content": "Hello to you too! How can I help you today?"}}]
|
20 |
+
self.context = "You are a helpful assistant"
|
21 |
|
22 |
+
def generate_template(question,task_id):
|
23 |
+
contexts=search(question,task_id=task_id)
|
24 |
+
context_yaml = ""
|
25 |
+
for context in contexts:
|
26 |
+
context_yaml += "\n"+ yaml.dump(context)
|
27 |
+
Template =f'''
|
28 |
+
#Instructions
|
29 |
+
You are given the following context in yaml of a transcript of a youtube video, the start and end times are indicated and the text that was said is also given. You are also given a question, use the context to answer the question in a consise manner, make it short and to the point, don't provide additional details.
|
30 |
+
Make it short and to the point, not more than 1 PARAGRAPH.
|
31 |
|
32 |
+
#Context
|
33 |
+
{context_yaml}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
+
#Question
|
36 |
+
{question}
|
37 |
+
'''
|
38 |
+
return Template
|
39 |
+
|
40 |
+
async def generate_message(self, messages,task_id):
|
41 |
+
latest_message = messages[-1]['content']
|
42 |
+
latest_message={"content":self.generate_template(latest_message,task_id)}
|
43 |
+
messages[-1]=latest_message
|
44 |
+
url = f'https://generativelanguage.googleapis.com/v1beta3/models/{self.model}:generateMessage?key={self.api_key}'
|
45 |
+
data = {
|
46 |
+
"prompt": {
|
47 |
+
"context": self.context,
|
48 |
+
"examples": self.examples,
|
49 |
+
"messages": messages
|
50 |
+
},
|
51 |
+
"temperature": self.temperature,
|
52 |
+
"top_k": self.top_k,
|
53 |
+
"top_p": self.top_p,
|
54 |
+
"candidate_count": self.candidate_count
|
55 |
+
}
|
56 |
+
|
57 |
+
async with aiohttp.ClientSession() as session:
|
58 |
+
async with session.post(url, json=data, headers={'Content-Type': 'application/json'}) as response:
|
59 |
+
try:
|
60 |
+
return await response.json()['candidates']
|
61 |
+
except Exception as e:
|
62 |
+
return f"Error ⚠️ {e}"
|
63 |
+
|
64 |
+
|
65 |
|