-
Notifications
You must be signed in to change notification settings - Fork 0
/
coho.py
99 lines (71 loc) · 3.05 KB
/
coho.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
from langchain_community.embeddings.cohere import CohereEmbeddings
from langchain_community.llms import Cohere
from langchain.prompts import PromptTemplate
from langchain.chains.question_answering import load_qa_chain
from langchain.chains import RetrievalQA
from langchain_community.vectorstores import Qdrant
from langchain_community.document_loaders import TextLoader
from langchain_community.document_loaders import PyPDFLoader
from dotenv import load_dotenv
import os
import flask as Flask, json
import requests
load_dotenv()
def auth():
cohere_api_key = os.getenv("cohere_api_key")
authorization = os.getenv("authorization")
model_url=os.getenv("model_url")
return cohere_api_key,authorization,model_url
def reader_bot(file_path):
with open(file_path, "rb") as f:
loader = PyPDFLoader(f.name)
pages = loader.load_and_split()
return pages
def get_summary(pages,cohere_api_key,company,poc,role):
embeddings = CohereEmbeddings(model="embed-english-v3.0", cohere_api_key=cohere_api_key)
store = Qdrant.from_documents(
pages,
embeddings,
location=":memory:",
collection_name="my_documents",
distance_func="cosine",
)
prompt_template = """Text: {context}
Question: {question}
You are replying as an multilingual AI Chat Bot,
answer the question without using any vulgularity and in maximum 50 words.Do not make your own answers and refer only to the pdf passed."""
PROMPT = PromptTemplate(
template=prompt_template, input_variables=["context", "question"]
)
chain_type_kwargs = {"prompt": PROMPT}
company
poc
role
question = f'''Give me a 100 word email message which we have to send to {poc},to apply for a {role} job in {company}. Do not add salutations.'''
if question:
chain_type_kwargs = {"prompt": PROMPT}
qa = RetrievalQA.from_chain_type(
llm=Cohere(model="command", temperature=0, cohere_api_key=cohere_api_key),
chain_type="stuff",
retriever=store.as_retriever(),
chain_type_kwargs=chain_type_kwargs,
return_source_documents=True,
)
answer = qa.invoke({"query": question})
result = answer["result"].replace("\n", "").replace("Answer:", "").replace("mentioned in the text:","").replace("According to the text you provided,","").replace("According to the provided text, ","")
lines = result.split('.')
# Remove the last line
updated_text = '.'.join(lines[:-1])
updated_text=updated_text+"."
updated_text=updated_text[1:]
quote_index = updated_text.find('"')
# Extract text after the double quotes
extracted_text = updated_text[quote_index + 1:]
return extracted_text
def get_pdf(url):
response = requests.get(url)
with open('sample.pdf', 'wb') as f:
f.write(response.content)
# cohere_api_key, _, _ = auth()
# pages = reader_bot()
# result = get_summary(pages, cohere_api_key)