Spaces:
Runtime error
Runtime error
Update kadiApy_ragchain.py
Browse files- kadiApy_ragchain.py +2 -106
kadiApy_ragchain.py
CHANGED
|
@@ -23,20 +23,11 @@ class KadiApyRagchain:
|
|
| 23 |
|
| 24 |
# Retrieve contexts
|
| 25 |
print("Start retrieving:")
|
| 26 |
-
#doc_contexts = self.retrieve_contexts(query, k=2, filter={"dataset_category": "kadi_apy_docs"})
|
| 27 |
-
code_contexts = self.retrieve_contexts(rewritten_query, k=3, filter={"usage": code_library_usage_prediction})
|
| 28 |
-
#code_contexts = self.retrieve_contexts(query, k=2)
|
| 29 |
|
| 30 |
-
#query_formulated_question= self.formulate_question(code_contexts)
|
| 31 |
-
#print("question:", query_formulated_question)
|
| 32 |
-
doc_contexts = self.retrieve_contexts(query, k=2, filter={"dataset_category": "kadi_apy_docs"})
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
#code_contexts = self.retrieve_contexts(query, k=5, filter={"dataset_category": "kadi_apy_source_code"})
|
| 37 |
|
| 38 |
-
# doc_contexts = self.retrieve_contexts(query, k=3, filter={"dataset_category": "kadi_apy_docs"})
|
| 39 |
-
# code_contexts = self.retrieve_contexts(rewritten_query, k=5, filter={"dataset_category": "kadi_apy_source_code"})
|
| 40 |
|
| 41 |
|
| 42 |
# Format contexts
|
|
@@ -92,39 +83,6 @@ class KadiApyRagchain:
|
|
| 92 |
"""
|
| 93 |
)
|
| 94 |
return self.llm.invoke(rewrite_prompt).content
|
| 95 |
-
|
| 96 |
-
def ask_rag(self, context):
|
| 97 |
-
rewrite_prompt = (
|
| 98 |
-
f""" "Code snippets" consists of methods and/or class declaration from the source code of an API Wrapper Python library.
|
| 99 |
-
Your task is to write a question how to use
|
| 100 |
-
|
| 101 |
-
Guidelines for rewriting the query:
|
| 102 |
-
1. Identify the main action the user wants to perform (e.g., "Upload a file to a record," "Get users of a group").
|
| 103 |
-
2. Remove conversational elements like greetings or pleasantries (e.g., "Hello Chatbot", "I need you to help me with").
|
| 104 |
-
3. Exclude specific variable values (e.g., "ID of my record is '31'") unless essential to the intent.
|
| 105 |
-
4. Rephrase the query to match the format and keywords used in the docstrings, focusing on verbs and objects relevant to the action (e.g., "Add a record to a collection").
|
| 106 |
-
5. Given the query the user might need more than one action to achieve his goal. In this case the rewritten query has more than one action.
|
| 107 |
-
|
| 108 |
-
Examples:
|
| 109 |
-
- User query: "Create a Python script with a method that facilitates the creation of records. This method should accept an array of identifiers as a parameter and allow metadata to be added to each record."
|
| 110 |
-
- Rewritten query: "create records, add metadata to record"
|
| 111 |
-
- User query: "Hi, can you help me write Python code to add a record to a collection? The record ID is '45', and the collection ID is '12'."
|
| 112 |
-
Rewritten query: "add a record to a collection"
|
| 113 |
-
- User query: I need a python script with which i create a new record with the title: "Hello World" and then link the record to a given collection.
|
| 114 |
-
Rewritten query: "create a new record with title" , "link a record to a collection"
|
| 115 |
-
|
| 116 |
-
Based on these examples and guidelines, rewrite the following user query to align more effectively with the keywords used in the docstrings.
|
| 117 |
-
Do not include any addition comments, explanations, or text.
|
| 118 |
-
|
| 119 |
-
Original query:
|
| 120 |
-
{query}
|
| 121 |
-
"""
|
| 122 |
-
)
|
| 123 |
-
return self.llm.invoke(rewrite_prompt).content
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
|
| 129 |
def predict_library_usage(self, query):
|
| 130 |
"""
|
|
@@ -188,68 +146,6 @@ class KadiApyRagchain:
|
|
| 188 |
"""
|
| 189 |
return self.llm.invoke(prompt).content
|
| 190 |
|
| 191 |
-
|
| 192 |
-
# def generate_response(self, query, chat_history, context):
|
| 193 |
-
# """
|
| 194 |
-
# Generate a response using the retrieved contexts and the LLM.
|
| 195 |
-
# """
|
| 196 |
-
# formatted_history = self.format_history(chat_history)
|
| 197 |
-
|
| 198 |
-
# # Update the prompt with history included
|
| 199 |
-
# prompt = f"""
|
| 200 |
-
# You are a Python programming assistant specialized in the "Kadi-APY" library.
|
| 201 |
-
# The "Kadi-APY" library is a Python package designed to facilitate interaction with the REST-like API of a software platform called Kadi4Mat.
|
| 202 |
-
# Your task is to answer the user's query based on the guidelines, and if needed, combine understanding provided by "Context"
|
| 203 |
-
# "Context" contains snippets from the source code and/or code examples
|
| 204 |
-
|
| 205 |
-
# Guidelines if generating code:
|
| 206 |
-
# - Display the complete code first, followed by a concise explanation in no more than 5 sentences.
|
| 207 |
-
|
| 208 |
-
# General Guidelines:
|
| 209 |
-
# - Refer to the "Chat History" if it provides context that could enhance your understanding of the user's query.
|
| 210 |
-
# - Always include the "Chat History" if relevant to the user's query for continuity and clarity in responses.
|
| 211 |
-
# - If the user's query cannot be fulfilled based on the provided snippets, reply with "The API does not support the requested functionality."
|
| 212 |
-
# - If the user's query does not implicate any task, reply with a question asking the user to elaborate.
|
| 213 |
-
|
| 214 |
-
# Chat History:
|
| 215 |
-
# {formatted_history}
|
| 216 |
-
|
| 217 |
-
# Context:
|
| 218 |
-
# {context}
|
| 219 |
-
|
| 220 |
-
# Query:
|
| 221 |
-
# {query}
|
| 222 |
-
# """
|
| 223 |
-
# return self.llm.invoke(prompt).content
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
def formulate_question(self, code_contexts):
|
| 229 |
-
"""
|
| 230 |
-
Generate a response using the retrieved contexts and the LLM.
|
| 231 |
-
"""
|
| 232 |
-
prompt = f"""
|
| 233 |
-
You are a Python programming assistant specialized in the "Kadi-APY" library.
|
| 234 |
-
The "Kadi-APY" library is a Python package designed to facilitate interaction with the REST-like API of a software platform called Kadi4Mat.
|
| 235 |
-
Your task is to formulate the next logical question a programmer would ask themselves to implement the methods provided in "Code-contexts".
|
| 236 |
-
|
| 237 |
-
"Context" contains snippets from the source code and metadata that provide details about the method.
|
| 238 |
-
|
| 239 |
-
Guidelines for generating questions:
|
| 240 |
-
- The question should be specific to the programmer's intent of using the class of the methods within a Python script.
|
| 241 |
-
- Focus on determining the entry point of the class to which the method belongs.
|
| 242 |
-
- Avoid vague or general questions; be precise about the next actionable steps.
|
| 243 |
-
- Only generate the question, do not include any additional comments, explanations, or text.
|
| 244 |
-
|
| 245 |
-
Code-contexts:
|
| 246 |
-
{code_contexts}
|
| 247 |
-
"""
|
| 248 |
-
return self.llm.invoke(prompt).content
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
|
| 254 |
def format_documents(self, documents):
|
| 255 |
formatted_docs = []
|
|
|
|
| 23 |
|
| 24 |
# Retrieve contexts
|
| 25 |
print("Start retrieving:")
|
|
|
|
|
|
|
|
|
|
| 26 |
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
+
code_contexts = self.retrieve_contexts(rewritten_query, k=3, filter={"usage": code_library_usage_prediction})
|
| 29 |
+
doc_contexts = self.retrieve_contexts(query, k=2, filter={"dataset_category": "kadi_apy_docs"})
|
|
|
|
| 30 |
|
|
|
|
|
|
|
| 31 |
|
| 32 |
|
| 33 |
# Format contexts
|
|
|
|
| 83 |
"""
|
| 84 |
)
|
| 85 |
return self.llm.invoke(rewrite_prompt).content
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
def predict_library_usage(self, query):
|
| 88 |
"""
|
|
|
|
| 146 |
"""
|
| 147 |
return self.llm.invoke(prompt).content
|
| 148 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
|
| 150 |
def format_documents(self, documents):
|
| 151 |
formatted_docs = []
|