-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathprompt_flow.py
53 lines (40 loc) · 1.68 KB
/
prompt_flow.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from openai import OpenAI
from coderag.config import OPENAI_API_KEY, OPENAI_CHAT_MODEL
from coderag.search import search_code
client = OpenAI(api_key=OPENAI_API_KEY)
SYSTEM_PROMPT = """
You are an expert coding assistant. Your task is to help users with their question. Use the retrieved code context to inform your responses, but feel free to suggest better solutions if appropriate.
"""
PRE_PROMPT = """
Based on the user's query and the following code context, provide a helpful response. If improvements can be made, suggest them with explanations.
User Query: {query}
Retrieved Code Context:
{code_context}
Your response:
"""
def execute_rag_flow(user_query):
try:
# Perform code search
search_results = search_code(user_query)
if not search_results:
return "No relevant code found for your query."
# Prepare code context
code_context = "\n\n".join([
f"File: {result['filename']}\n{result['content']}"
for result in search_results[:3] # Limit to top 3 results
])
# Construct the full prompt
full_prompt = PRE_PROMPT.format(query=user_query, code_context=code_context)
# Generate response using OpenAI
response = client.chat.completions.create(
model=OPENAI_CHAT_MODEL,
messages=[
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": full_prompt}
],
temperature=0.3,
max_tokens=4000
)
return response.choices[0].message.content.strip()
except Exception as e:
return f"Error in RAG flow execution: {e}"