MCPcopy Create free account
hub / github.com/clockworknowledge/menome_processor / chatSourcesquestion

Function chatSourcesquestion

app/routers/chat.py:116–179  ·  view source on GitHub ↗
(question: str = Query(..., description="The question to be processed"), current_user: User = Depends(get_current_user))

Source from the content-addressed store, hash-verified

114 description="This endpoint provides a chat response along with sources of information. It uses the ChatOpenAI model for generating responses.",
115 tags=["Chat", "Sources"])
116def chatSourcesquestion(question: str = Query(..., description="The question to be processed"), current_user: User = Depends(get_current_user)):
117 driver = GraphDatabase.driver(uri, auth=(AppConfig.NEO4J_USER, AppConfig.NEO4J_PASSWORD))
118
119 # Start measuring time
120 start_time = time.time()
121 request_payload = json.dumps({"question": question}).encode('utf-8')
122 request_payload_size = sys.getsizeof(request_payload)
123
124 # Generate a response in chatGPT style based on the user's question
125 chain = RetrievalQAWithSourcesChain.from_chain_type(
126 ChatOpenAI(temperature=1, max_tokens=4000, model_name="gpt-4-1106-preview", openai_api_key=AppConfig.OPENAI_API_KEY),
127 chain_type="stuff",
128 retriever=typical_vectorstore.as_retriever(search_kwargs={"k": 5, 'score_threshold': 0.5})
129 )
130
131 # Measure time after setting up the chain
132 setup_time = time.time()
133
134 langchain_response = chain({"question": question}, return_only_outputs=False)
135
136
137 # Measure time after getting the response
138 langchain_response_time = time.time()
139 langchain_response_payload = json.dumps(langchain_response).encode('utf-8')
140 langchain_response_payload_size = sys.getsizeof(langchain_response_payload)
141
142 # Extract 'answer' and 'sources' (UUIDs) from the chain response
143 answer = langchain_response.get('answer', '')
144 uuids = langchain_response.get('sources', [])
145
146 if isinstance(uuids, str):
147 uuids = [uuid.strip() for uuid in uuids.split(",")]
148
149 # Fetch node properties from Neo4j based on UUIDs
150 nodes_data = fetch_node_properties_by_uuid(driver, uuids)
151 nodes_data_payload = json.dumps(nodes_data).encode('utf-8')
152 nodes_data_payload_size = sys.getsizeof(nodes_data_payload)
153
154 # Measure time after fetching data from Neo4j
155 neo4j_fetch_time = time.time()
156
157 # Calculate elapsed times
158 setup_duration = setup_time - start_time
159 response_duration = langchain_response_time - setup_time
160 fetch_duration = neo4j_fetch_time - langchain_response_time
161 total_duration = neo4j_fetch_time - start_time
162
163 driver.close()
164
165 return {
166 "answer": answer,
167 "sources": nodes_data,
168 "timings": {
169 "setup_duration": setup_duration,
170 "langchain_response_duration": response_duration,
171 "neo4j_fetch_duration": fetch_duration,
172 "total_duration": total_duration
173 },

Callers

nothing calls this directly

Calls 1

Tested by

no test coverage detected