1111from langchain_core .output_parsers import StrOutputParser
1212from langchain_weaviate import WeaviateVectorStore
1313
14+ logging .basicConfig (level = logging .INFO )
15+ logger = logging .getLogger (__name__ )
16+
1417app = Flask (__name__ )
1518
16- openlit .init ()
19+ openlit .init (environment = "llm-app" )
1720
1821# Read environment variables
1922INSTRUCT_MODEL_URL = os .getenv ('INSTRUCT_MODEL_URL' ) # i.e. http://localhost:8000/v1
4043@app .route ("/askquestion" , methods = ['POST' ])
4144def ask_question ():
4245
43- data = request .json
44- question = data .get ('question' )
45-
46- weaviate_client = weaviate .connect_to_custom (
47- # url is: http://weaviate.weaviate.svc.cluster.local:80
48- http_host = os .getenv ('WEAVIATE_HTTP_HOST' ),
49- http_port = os .getenv ('WEAVIATE_HTTP_PORT' ),
50- http_secure = False ,
51- grpc_host = os .getenv ('WEAVIATE_GRPC_HOST' ),
52- grpc_port = os .getenv ('WEAVIATE_GRPC_PORT' ),
53- grpc_secure = False
54- )
55-
56- # connect with the vector store that was populated earlier
57- vector_store = WeaviateVectorStore (
58- client = weaviate_client ,
59- embedding = embeddings_model ,
60- index_name = "CustomDocs" ,
61- text_key = "page_content"
62- )
63-
64- chain = (
65- {
66- "context" : vector_store .as_retriever (),
67- "question" : RunnablePassthrough ()
68- }
69- | prompt
70- | llm
71- | StrOutputParser ()
72- )
73-
74- # Get the schema which contains all collections
75- schema = weaviate_client .collections .list_all ()
76-
77- logger .info ("Available collections in Weaviate:" )
78- for collection_name , collection_config in schema .items ():
79- print (f"- { collection_name } " )
80-
81- response = chain .invoke (question )
82- logger .info (response )
83-
84- weaviate_client .close ()
85-
86- return response
46+ logger .info (f"Responding to question" )
47+ try :
48+ data = request .json
49+ question = data .get ('question' )
50+
51+ weaviate_client = weaviate .connect_to_custom (
52+ # url is: http://weaviate.weaviate.svc.cluster.local:80
53+ http_host = os .getenv ('WEAVIATE_HTTP_HOST' ),
54+ http_port = os .getenv ('WEAVIATE_HTTP_PORT' ),
55+ http_secure = False ,
56+ grpc_host = os .getenv ('WEAVIATE_GRPC_HOST' ),
57+ grpc_port = os .getenv ('WEAVIATE_GRPC_PORT' ),
58+ grpc_secure = False
59+ )
60+
61+ # connect with the vector store that was populated earlier
62+ vector_store = WeaviateVectorStore (
63+ client = weaviate_client ,
64+ embedding = embeddings_model ,
65+ index_name = "CustomDocs" ,
66+ text_key = "page_content"
67+ )
68+
69+ chain = (
70+ {
71+ "context" : vector_store .as_retriever (),
72+ "question" : RunnablePassthrough ()
73+ }
74+ | prompt
75+ | llm
76+ | StrOutputParser ()
77+ )
78+
79+ # Get the schema which contains all collections
80+ schema = weaviate_client .collections .list_all ()
81+
82+ logger .info ("Available collections in Weaviate:" )
83+ for collection_name , collection_config in schema .items ():
84+ print (f"- { collection_name } " )
85+
86+ response = chain .invoke (question )
87+ logger .info (response )
88+
89+ weaviate_client .close ()
90+
91+ return response
92+
93+ except Exception as e :
94+ logger .error (f"Error responding to question: { e } " )
95+ return None
0 commit comments