22
33import openai
44from openai .types .chat import ChatCompletion
5- from pydantic import BaseModel , Field
5+ from pydantic import BaseModel , Field , ValidationError
66
77from vectorcode .cli_utils import Config
88from vectorcode .rewriter .base import RewriterBase
@@ -22,7 +22,9 @@ def __init__(self, config: Config) -> None:
2222 self .client = openai .Client (
2323 ** self .config .rewriter_params .get ("client_kwargs" , {})
2424 )
25- self .system_prompt = """
25+ self .system_prompt = self .config .rewriter_params .get (
26+ "system_prompt" ,
27+ """
2628Role:
2729 You are a code-aware rewriter that improves technical queries/docs for retrieval. Never assume a programming language unless the input explicitly includes syntax, APIs, or error messages from one.
2830Rules:
@@ -33,6 +35,8 @@ def __init__(self, config: Config) -> None:
3335
3436 Omit langauge-specific keywords (e.g., "async def foo():" → "foo")
3537
38+ Do not include standard libraries the query.
39+
3640 For Docs/Code:
3741
3842 Clarify ambiguous terms only with explicit context.
@@ -46,26 +50,35 @@ def __init__(self, config: Config) -> None:
4650 No code changes.
4751
4852 No hallucinations.
49- """
53+ """ ,
54+ )
5055
5156 async def rewrite (self , original_query : list [str ]):
52- comp : ChatCompletion = self .client .beta .chat .completions .parse (
53- messages = [
54- {"role" : "system" , "content" : self .system_prompt },
55- {"role" : "user" , "content" : " " .join (original_query )},
56- ],
57- response_format = _NewQuery ,
58- ** self .config .rewriter_params .get ("completion_kwargs" , {}),
59- )
60- if comp is None or len (comp .choices ) == 0 :
61- logger .info ("Recieved no rewritten query. Fallingback to original_query." )
62- return original_query
63- choice = comp .choices [0 ].message
64- if choice and choice .parsed :
65- logger .debug (f"Rewritten queries to: { choice .parsed } " )
66- return choice .parsed .keywords
67- else :
57+ try :
58+ comp : ChatCompletion = self .client .beta .chat .completions .parse (
59+ messages = [
60+ {"role" : "system" , "content" : self .system_prompt },
61+ {"role" : "user" , "content" : " " .join (original_query )},
62+ ],
63+ response_format = _NewQuery ,
64+ ** self .config .rewriter_params .get ("completion_kwargs" , {}),
65+ )
66+ if comp is None or len (comp .choices ) == 0 :
67+ logger .info (
68+ "Recieved no rewritten query. Fallingback to original_query."
69+ )
70+ return original_query
71+ choice = comp .choices [0 ].message
72+ if choice and choice .parsed :
73+ logger .debug (f"Rewritten queries to: { choice .parsed } " )
74+ return choice .parsed .keywords
75+ else :
76+ logger .warning (
77+ f"Failed to parse structured output: { choice .refusal } . Fallingback to original_query."
78+ )
79+ return original_query
80+ except ValidationError :
6881 logger .warning (
69- f "Failed to parse structured output: { choice . refusal } . Fallingback to original_query."
82+ "Failed to parse structured output. Fallingback to original_query."
7083 )
7184 return original_query
0 commit comments