bupa1018 commited on
Commit
c18ba93
·
1 Parent(s): 4a6e0d5

Create ragchain.py

Browse files
Files changed (1) hide show
  1. ragchain.py +86 -0
ragchain.py ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ class RAGChain:
2
+
3
+ def __init__(self, llm, vector_store):
4
+ """
5
+ Initialize the RAGChain with an LLM instance and a vector store.
6
+ """
7
+ self.llm = llm
8
+ self.vector_store = vector_store
9
+
10
+ def predict_library_usage(self, query):
11
+ """
12
+ Use the LLM to predict the relevant library for the user's query.
13
+ """
14
+ prompt = (
15
+ f"""The query is: '{query}'.
16
+ Based on the user's query, assist them by determining which technical document they should read to interact with the software named 'Kadi4Mat'.
17
+ There are three different technical documents to choose from:
18
+ - Document 1: Provides information on how to use a Python library to interact with the HTTP API of 'Kadi4Mat'.
19
+ - Document 2: Provides information on how to use a Python library to implement custom CLI commands to interact with 'Kadi4Mat'.
20
+
21
+ Your task is to select the single most likely option.
22
+ If Document 1 is the best choice, respond with 'kadi-apy python library'.
23
+ If Document 2 is the best choice, respond with 'kadi-apy python cli library'.
24
+ Respond with only the exact corresponding option and do not include any additional comments, explanations, or text."
25
+ """
26
+ )
27
+ return self.llm.predict(prompt)
28
+
29
+ def retrieve_contexts(self, query, library_usage_prediction):
30
+ """
31
+ Retrieve relevant documents and source code based on the query and library usage prediction.
32
+ """
33
+ doc_contexts = self.vector_store.similarity_search(query, k=5, filter={"usage": "doc"})
34
+ code_contexts = self.vector_store.similarity_search(query, k=5, filter={"usage": library_usage_prediction})
35
+
36
+ return doc_contexts, code_contexts
37
+
38
+ def format_context(self, doc_contexts, code_contexts):
39
+ """
40
+ Format the retrieved document and code contexts.
41
+ """
42
+ doc_context = format_kadi_api_doc_context(doc_contexts)
43
+ code_context = format_kadi_apy_library_context(code_contexts)
44
+
45
+ return doc_context, code_context
46
+
47
+ def generate_response(self, query, doc_context, code_context):
48
+ """
49
+ Generate a response using the retrieved contexts and the LLM.
50
+ """
51
+ prompt = f"""You are an expert python developer. You are assisting in generating code for users who want to programmatically
52
+ make use of api of a software. There is a specific Python library named "kadiAPY" designed to interact with
53
+ the API of the software. It provides an object-oriented approach for interfacing with the API.
54
+
55
+ You are given "Documentation Snippets" and "Code Snippets"
56
+ "Documentation Snippets:" Contains a collection of potentially useful snippets, including code examples and documentation excerpts of "kadiAPY"
57
+ "Code Snippets:" Contains potentially useful snippets from the source code of "kadiAPY"
58
+
59
+ Based on the retrieved snippets and the guidelines answer the "query".
60
+
61
+ General Guidelines:
62
+ - If no related information is found from the snippets to answer the query, reply that you do not know.
63
+
64
+ Guidelines when generating code:
65
+ - First display the full code and then follow with a well structured explanation of the generated code.
66
+
67
+ Documentation Snippets:
68
+ {doc_context}
69
+ Code Snippets:
70
+ {code_context}
71
+
72
+ Query:
73
+ {query}
74
+ """
75
+ return self.llm.invoke(prompt).content
76
+
77
+ def rag_workflow(self, query):
78
+ """
79
+ Complete the RAG workflow: predict library usage, retrieve contexts, and generate a response.
80
+ """
81
+ library_usage_prediction = self.predict_library_usage(query)
82
+
83
+ doc_contexts, code_contexts = self.retrieve_contexts(query, library_usage_prediction)
84
+ doc_context, code_context = self.format_context(doc_contexts, code_contexts)
85
+
86
+ return self.generate_response(query, doc_context, code_context)