Siddartha10 commited on
Commit
8cd577e
·
verified ·
1 Parent(s): 1c30a6b

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -242
app.py DELETED
@@ -1,242 +0,0 @@
1
- import os
2
- import streamlit as st
3
- from typing import List, Tuple
4
- import json
5
- import uvicorn
6
- from dotenv import load_dotenv
7
- load_dotenv()
8
- from fastapi import FastAPI
9
- from langchain.agents import AgentExecutor
10
- from langchain.agents.format_scratchpad import format_to_openai_function_messages
11
- from langchain.agents.output_parsers import OpenAIFunctionsAgentOutputParser
12
- from langchain.callbacks import FinalStreamingStdOutCallbackHandler
13
- from langchain.chat_models import ChatOpenAI
14
- from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder
15
- from langchain.pydantic_v1 import BaseModel, Field
16
- from langchain.schema.messages import AIMessage, HumanMessage
17
- from langchain.tools.render import format_tool_to_openai_function
18
- from langchain_community.utilities.google_serper import GoogleSerperAPIWrapper
19
- from langchain_core.runnables import ConfigurableField
20
- from langchain_core.tools import Tool
21
- from langserve import add_routes
22
- from langchain.prompts import PromptTemplate
23
- import requests
24
- from langchain.embeddings.openai import OpenAIEmbeddings
25
- from langchain.vectorstores import Qdrant
26
- from langchain.chains import RetrievalQA
27
- from langchain.agents import Tool, Agent, AgentType
28
- from langchain.agents import AgentExecutor
29
- from langchain_core.tools import Tool
30
- from langchain_openai import ChatOpenAI
31
- from langchain_openai import AzureChatOpenAI
32
- from langchain_community.document_loaders import JSONLoader
33
-
34
- embeddings = OpenAIEmbeddings()
35
- llm_1 = AzureChatOpenAI(openai_api_version=os.environ.get("AZURE_OPENAI_VERSION", "2023-07-01-preview"),
36
- azure_deployment=os.environ.get("AZURE_OPENAI_DEPLOYMENT", "gpt4chat"),
37
- azure_endpoint=os.environ.get("AZURE_OPENAI_ENDPOINT", "https://gpt-4-trails.openai.azure.com/"),
38
- api_key=os.environ.get("AZURE_OPENAI_KEY"))
39
-
40
- llm = ChatOpenAI(temperature=0.2,
41
- model="gpt-3.5-turbo-0125",
42
- streaming=True,
43
- callbacks=[FinalStreamingStdOutCallbackHandler()]).configurable_fields(
44
- temperature=ConfigurableField(
45
- id="llm_temperature",
46
- name="LLM Temperature",
47
- description="The temperature of the LLM"))
48
- assistant_system_message = """You are a helpful assistant. \
49
- Use tools (only if necessary) to best answer the users questions."""
50
-
51
- prompt = ChatPromptTemplate.from_messages(
52
- [
53
- ("system", assistant_system_message),
54
- MessagesPlaceholder(variable_name="chat_history"),
55
- ("user", "{input}"),
56
- MessagesPlaceholder(variable_name="agent_scratchpad"),
57
- ]
58
- )
59
-
60
- # Define the API call function for Ares API
61
-
62
- def api_call(text):
63
- url = "https://api-ares.traversaal.ai/live/predict"
64
-
65
- payload = { "query": [text]}
66
- headers = {
67
- "x-api-key": "ares_a0866ad7d71d2e895c5e05dce656704a9e29ad37860912ad6a45a4e3e6c399b5",
68
- "content-type": "application/json"
69
- }
70
-
71
- response = requests.post(url, json=payload, headers=headers)
72
-
73
- # here we will use the llm to summarize the response received from the ares api
74
- response_data = response.json()
75
- #print(response_data)
76
- try:
77
- response_text = response_data['data']['response_text']
78
- web_urls = response_data['data']['web_url']
79
- # Continue processing the data...
80
- except KeyError:
81
- print("Error: Unexpected response from the API. Please try again or contact the api owner.")
82
- # Optionally, you can log the error or perform other error handling actions.
83
-
84
-
85
- if len(response_text) > 10000:
86
- response_text = response_text[:8000]
87
- prompt = f"Summarize the following text in 500-100 0 words and jsut summarize what you see and do not add anythhing else: {response_text}"
88
- summary = llm_1.invoke(prompt)
89
- print(summary)
90
- else:
91
- summary = response_text
92
-
93
- result = "{} My list is: {}".format(response_text, web_urls)
94
-
95
- # Convert the result to a string
96
- result_str = str(result)
97
-
98
- return result_str
99
-
100
-
101
-
102
- def metadata_func(record: str, metadata: dict) -> dict:
103
- lines = record.split('\n')
104
- locality_line = lines[10]
105
- price_range_line = lines[12]
106
- locality = locality_line.split(': ')[1]
107
- price_range = price_range_line.split(': ')[1]
108
- metadata["location"] = locality
109
- metadata["price_range"] = price_range
110
-
111
- return metadata
112
-
113
- # Instantiate the JSONLoader with the metadata_func
114
- jq_schema = '.parser[] | to_entries | map("\(.key): \(.value)") | join("\n")'
115
- loader = JSONLoader(
116
- jq_schema=jq_schema,
117
- file_path='data.json',
118
- metadata_func=metadata_func,
119
- )
120
-
121
- # Load the JSON file and extract metadata
122
- documents = loader.load()
123
-
124
-
125
- from langchain.vectorstores import FAISS
126
- def get_vectorstore(text_chunks):
127
- # Check if the FAISS index file already exists
128
- if os.path.exists("faiss_index"):
129
- # Load the existing FAISS index
130
- vectorstore = FAISS.load_local("faiss_index")
131
- print("Loaded existing FAISS index.")
132
- else:
133
- # Create a new FAISS index
134
- embeddings = OpenAIEmbeddings()
135
- vectorstore = FAISS.from_documents(documents=text_chunks, embedding=embeddings)
136
- # Save the new FAISS index locally
137
- vectorstore.save_local("faiss_index")
138
- print("Created and saved new FAISS index.")
139
- return vectorstore
140
-
141
- #docs = new_db.similarity_search(query)
142
-
143
- vector = get_vectorstore(documents)
144
-
145
- template = """
146
-
147
- context:- I have low budget what is the best hotel in Instanbul?
148
- anser:- The other hotels in instanbul are costly and are not in your budget. so the best hotel in instanbul for you is hotel is xyz."
149
-
150
- Don’t give information not mentioned in the CONTEXT INFORMATION.
151
- The system should take into account various factors such as location, amenities, user reviews, and other relevant criteria to
152
- generate informative and personalized explanations.
153
- {context}
154
- Question: {question}
155
- Answer:"""
156
-
157
- def search():
158
- #llm = ChatOpenAI(model="gpt-3.5-turbo-1106", temperature=0)
159
- vector = vector
160
- prompt = PromptTemplate(template=template, input_variables=["context","question"])
161
- chain_type_kwargs = {"prompt": prompt}
162
- return RetrievalQA.from_chain_type(
163
- llm=llm,
164
- chain_type="stuff",
165
- retriever=vector.as_retriever(),
166
- chain_type_kwargs=chain_type_kwargs,
167
- )
168
-
169
- # Initialize LangChain tools
170
-
171
-
172
- api_tool = Tool(name="Ares_API",
173
- func=api_call,
174
- description="Integration with Traversaal AI Ares API for real-time internet searches."
175
- )
176
-
177
- chain_rag_tool = Tool(name="RAG_Chain",
178
- func=search,
179
- description="RAG chain for question answering."
180
- )
181
-
182
- app = FastAPI(
183
- title='Example',
184
- )
185
-
186
- tools = [chain_rag_tool, api_tool]
187
- llm_with_tools = llm.bind(functions=[format_tool_to_openai_function(t) for t in tools])
188
-
189
-
190
- def _format_chat_history(chat_history: List[Tuple[str, str]]):
191
- buffer = []
192
- for human, ai in chat_history:
193
- buffer.append(HumanMessage(content=human))
194
- buffer.append(AIMessage(content=ai))
195
- return buffer
196
-
197
-
198
- agent = (
199
- {
200
- "input": lambda x: x["input"],
201
- "chat_history": lambda x: _format_chat_history(x["chat_history"]),
202
- "agent_scratchpad": lambda x: format_to_openai_function_messages(
203
- x["intermediate_steps"]
204
- ),
205
- }
206
- | prompt
207
- | llm_with_tools
208
- | OpenAIFunctionsAgentOutputParser()
209
- )
210
-
211
-
212
- class AgentInput(BaseModel):
213
- input: str
214
- chat_history: List[Tuple[str, str]] = Field(
215
- ..., extra={"widget": {"type": "chat", "input": "input", "output": "output"}}
216
- )
217
-
218
-
219
- agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True).with_types(
220
- input_type=AgentInput
221
- )
222
-
223
-
224
- def get_response(user_input):
225
- response = agent_executor.invoke({"input":user_input, "chat_history": _format_chat_history([])})
226
- return response
227
-
228
-
229
- def main():
230
- st.title("Travle Assistant Chatbot")
231
- st.write("Welcome to the Hotel Assistant Chatbot!")
232
- user_input = st.text_input("User Input:")
233
-
234
- if st.button("Submit"):
235
- response = get_response(user_input)
236
- st.text_area("Chatbot Response:", value=response)
237
-
238
- if st.button("Exit"):
239
- st.stop()
240
-
241
- if __name__ == "__main__":
242
- main()