SungBeom commited on
Commit
4d3a8e4
1 Parent(s): 406bea4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -4
app.py CHANGED
@@ -1,7 +1,7 @@
1
  import os
2
  import configparser
3
 
4
- from typing import List, Union, Optional, Any, Dict
5
  import re
6
  import sys
7
  import time
@@ -14,9 +14,11 @@ import threading
14
  import pandas as pd
15
  from langchain import SerpAPIWrapper, LLMChain
16
  from langchain.agents import Tool, AgentType, AgentExecutor, LLMSingleActionAgent, AgentOutputParser
 
17
  from langchain.callbacks.streaming_stdout_final_only import FinalStreamingStdOutCallbackHandler
18
  from langchain.chat_models import ChatOpenAI
19
  from langchain.chains import LLMChain, SimpleSequentialChain
 
20
  from langchain.chains.query_constructor.base import AttributeInfo
21
  from langchain.document_loaders import DataFrameLoader, SeleniumURLLoader
22
  from langchain.embeddings import OpenAIEmbeddings
@@ -24,7 +26,7 @@ from langchain.indexes import VectorstoreIndexCreator
24
  from langchain.prompts import PromptTemplate, StringPromptTemplate, load_prompt, BaseChatPromptTemplate
25
  from langchain.llms import OpenAI
26
  from langchain.retrievers.self_query.base import SelfQueryRetriever
27
- from langchain.schema import AgentAction, AgentFinish, HumanMessage
28
  from langchain.vectorstores import Chroma
29
 
30
  import gradio as gr
@@ -112,7 +114,30 @@ metadata_field_info = [
112
  wine_vectorstore = Chroma.from_documents(docs, embeddings)
113
  document_content_description = "A database of wines. 'name' and 'pairing' must be included in the query, and 'Body', 'Tannin', 'Sweetness', 'Alcohol', 'Price', 'Rating', 'Wine_Type', and 'Country' can be included in the filter. query and filter must be form of 'key: value'. For example, query: 'name: 돔페리뇽, pairing:육류'."
114
  llm = OpenAI(temperature=0)
115
- wine_retriever = SelfQueryRetriever.from_llm(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
116
  llm, wine_vectorstore, document_content_description, metadata_field_info, verbose=True
117
  ) # Added missing closing parenthesis
118
 
@@ -150,7 +175,7 @@ metadata_field_info = [
150
 
151
  document_content_description = "Database of a winebar"
152
  llm = OpenAI(temperature=0)
153
- wine_bar_retriever = SelfQueryRetriever.from_llm(
154
  llm, wine_bar_vectorstore, document_content_description, metadata_field_info=metadata_field_info, verbose=True
155
  ) # Added missing closing parenthesis
156
  #### Tool3: Search in Google
 
1
  import os
2
  import configparser
3
 
4
+ from typing import List, Union, Optional, Any, Dict, cast
5
  import re
6
  import sys
7
  import time
 
14
  import pandas as pd
15
  from langchain import SerpAPIWrapper, LLMChain
16
  from langchain.agents import Tool, AgentType, AgentExecutor, LLMSingleActionAgent, AgentOutputParser
17
+ from langchain.callbacks.manager import Callbacks
18
  from langchain.callbacks.streaming_stdout_final_only import FinalStreamingStdOutCallbackHandler
19
  from langchain.chat_models import ChatOpenAI
20
  from langchain.chains import LLMChain, SimpleSequentialChain
21
+ from langchain.chains.query_constructor.ir import StructuredQuery
22
  from langchain.chains.query_constructor.base import AttributeInfo
23
  from langchain.document_loaders import DataFrameLoader, SeleniumURLLoader
24
  from langchain.embeddings import OpenAIEmbeddings
 
26
  from langchain.prompts import PromptTemplate, StringPromptTemplate, load_prompt, BaseChatPromptTemplate
27
  from langchain.llms import OpenAI
28
  from langchain.retrievers.self_query.base import SelfQueryRetriever
29
+ from langchain.schema import AgentAction, AgentFinish, HumanMessage, Document
30
  from langchain.vectorstores import Chroma
31
 
32
  import gradio as gr
 
114
  wine_vectorstore = Chroma.from_documents(docs, embeddings)
115
  document_content_description = "A database of wines. 'name' and 'pairing' must be included in the query, and 'Body', 'Tannin', 'Sweetness', 'Alcohol', 'Price', 'Rating', 'Wine_Type', and 'Country' can be included in the filter. query and filter must be form of 'key: value'. For example, query: 'name: 돔페리뇽, pairing:육류'."
116
  llm = OpenAI(temperature=0)
117
+
118
+ class CustomSelfQueryRetriever(SelfQueryRetriever):
119
+ async def aget_relevant_documents(self, query: str, callbacks: Callbacks = None) -> List[Document]:
120
+ inputs = self.llm_chain.prep_inputs({"query": query})
121
+ structured_query = cast(
122
+ StructuredQuery,
123
+ self.llm_chain.predict_and_parse(callbacks=callbacks, **inputs),
124
+ )
125
+ if self.verbose:
126
+ print(structured_query)
127
+ new_query, new_kwargs = self.structured_query_translator.visit_structured_query(
128
+ structured_query
129
+ )
130
+ if structured_query.limit is not None:
131
+ new_kwargs["k"] = structured_query.limit
132
+
133
+ if self.use_original_query:
134
+ new_query = query
135
+
136
+ search_kwargs = {**self.search_kwargs, **new_kwargs}
137
+ docs = self.vectorstore.search(new_query, self.search_type, **search_kwargs)
138
+ return docs
139
+
140
+ wine_retriever = CustomSelfQueryRetriever.from_llm(
141
  llm, wine_vectorstore, document_content_description, metadata_field_info, verbose=True
142
  ) # Added missing closing parenthesis
143
 
 
175
 
176
  document_content_description = "Database of a winebar"
177
  llm = OpenAI(temperature=0)
178
+ wine_bar_retriever = CustomSelfQueryRetriever.from_llm(
179
  llm, wine_bar_vectorstore, document_content_description, metadata_field_info=metadata_field_info, verbose=True
180
  ) # Added missing closing parenthesis
181
  #### Tool3: Search in Google