@@ -305,17 +305,34 @@ def generate_completion(user_prompt, vector_search_results, chat_history):
305
305
return response.model_dump()
306
306
307
307
def chat_completion (cache_container , movies_container , user_input ):
308
+ print (" starting completion" )
309
+ # Generate embeddings from the user input
308
310
user_embeddings = generate_embeddings(user_input)
309
- cache_results = vector_search( container = cache_container, vectors = user_embeddings, similarity_score = 0.99 , num_results = 1 )
310
-
311
+ # Query the chat history cache first to see if this question has been asked before
312
+ cache_results = get_cache( container = cache_container, vectors = user_embeddings, similarity_score = 0.99 , num_results = 1 )
311
313
if len (cache_results) > 0 :
312
- return cache_results[0 ][' document' ][' completion' ], True
313
-
314
- search_results = vector_search(movies_container, user_embeddings)
315
- chat_history = get_chat_history(cache_container, completions = 3 )
316
- completions_results = generate_completion(user_input, search_results, chat_history)
317
- cache_response(cache_container, user_input, user_embeddings, completions_results)
318
- return completions_results[' choices' ][0 ][' message' ][' content' ], False
314
+ print (" Cached Result\n " )
315
+ return cache_results[0 ][' completion' ], True
316
+
317
+ else :
318
+ # perform vector search on the movie collection
319
+ print (" New result\n " )
320
+ search_results = vector_search(movies_container, user_embeddings)
321
+
322
+ print (" Getting Chat History\n " )
323
+ # chat history
324
+ chat_history = get_chat_history(cache_container, 3 )
325
+ # generate the completion
326
+ print (" Generating completions \n " )
327
+ completions_results = generate_completion(user_input, search_results, chat_history)
328
+
329
+ print (" Caching response \n " )
330
+ # cache the response
331
+ cache_response(cache_container, user_input, user_embeddings, completions_results)
332
+
333
+ print (" \n " )
334
+ # Return the generated LLM completion
335
+ return completions_results[' choices' ][0 ][' message' ][' content' ], False
319
336
```
320
337
321
338
### 11. Cache Generated Responses
0 commit comments