-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils_1.py
150 lines (123 loc) · 4.35 KB
/
utils_1.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
from langchain.embeddings import SentenceTransformerEmbeddings
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_community.document_loaders import WebBaseLoader
from langchain_community.vectorstores import Chroma
from langchain.chat_models import ChatOpenAI
from langchain.prompts import MessagesPlaceholder
from langchain.chains import RetrievalQA
from typing import Tuple, Dict
from langchain.memory import ConversationBufferMemory
from langchain.prompts.chat import MessagesPlaceholder
from langchain.agents import initialize_agent, Tool
from langchain.agents import AgentExecutor
from langchain_openai import OpenAIEmbeddings
from langchain_community.tools.tavily_search import TavilySearchResults
from dotenv import load_dotenv
import os
import io
import base64
import requests
from PIL import Image
import streamlit as st
load_dotenv()
os.environ['OPENAI_API_KEY'] = st.secrets['OPENAI_API_KEY']
os.environ['TAVILY_API_KEY'] = st.secrets['TAVILY_API_KEY']
tavily_api_key = os.environ['TAVILY_API_KEY']
openai_api_key = os.environ['OPENAI_API_KEY']
embeddings = OpenAIEmbeddings()
def encode_and_query_api(image, api_key):
buffered = io.BytesIO()
image.save(buffered, format="JPEG")
base64_image = base64.b64encode(buffered.getvalue()).decode("utf-8")
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {api_key}"
}
payload = {
"model": "gpt-4-vision-preview",
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "Please describe this image as good and authentic as possible , include all it details possible as it will be fed to and auto claim system to insurence companies"
},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{base64_image}"
}
}
]
}
],
"max_tokens": 300
}
response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
return response.json()['choices'][0]['message']['content']
urls = [
"https://en.wikipedia.org/wiki/Vehicle_insurance_in_the_United_States",
]
docs = [WebBaseLoader(url).load() for url in urls]
docs_list = [item for sublist in docs for item in sublist]
text_splitter = RecursiveCharacterTextSplitter.from_tiktoken_encoder(
chunk_size=250, chunk_overlap=0
)
doc_splits = text_splitter.split_documents(docs_list)
# Add to vectorDB
vectorstore = Chroma.from_documents(
documents=doc_splits,
collection_name="rag-chroma",
embedding=embeddings,
)
class Config():
"""
Contains the configuration of the LLM.
"""
model = 'gpt-3.5-turbo'
llm = ChatOpenAI(temperature=0, model=model)
cfg = Config()
qa = RetrievalQA.from_chain_type(
llm=cfg.llm,
chain_type="stuff",
retriever = vectorstore.as_retriever()
)
def setup_memory() -> Tuple[Dict, ConversationBufferMemory]:
"""
Sets up memory for the open ai functions agent.
:return a tuple with the agent keyword pairs and the conversation memory.
"""
agent_kwargs = {
"extra_prompt_messages": [MessagesPlaceholder(variable_name="memory")],
}
memory = ConversationBufferMemory(memory_key="memory", return_messages=True)
return agent_kwargs, memory
def setup_agent() -> AgentExecutor:
"""
Sets up the tools for a function based chain.
We have here the following tools:
"""
cfg = Config()
tools = [
Tool(
name="knowledge search",
func=qa.run,
description="useful for when you need more advanced search option to answer questions about insurence. "
),
Tool(
name='web search',
func=TavilySearchResults(api_key=tavily_api_key).run,
description=(
'''use this tool when you can't find the content in the knowledge base and you need more advenced search functionalities '''
))
]
agent_kwargs, memory = setup_memory()
return initialize_agent(
tools,
cfg.llm,
verbose=False,
agent_kwargs=agent_kwargs,
memory=memory,
handle_parsing_errors=True
)