@@ -28,29 +28,22 @@ You MUST generate EXACTLY 3 proposals about this query: "{query}"
2828}
2929"# ;
3030
31- const REVISE_PROMPT : & str = r#"You are an expert prompt engineer. A user wants to revise a prompt proposal.
32-
33- Original Proposal: "{proposal}"
34- User's Revision: "{revision}"
35-
36- Your task is to integrate the user's revision into the original proposal to create a new, single, improved prompt.
37- The new prompt should be self-contained and ready to use.
38-
39- Format your response as a JSON object with a single key "proposal" which is a string.
40- Example:
41- {
42- "proposal": "This is the new, revised prompt."
31+ #[ derive( Deserialize , Debug ) ]
32+ struct ProposalObject {
33+ context : String ,
34+ question : String ,
4335}
44- "# ;
4536
4637#[ derive( Deserialize , Debug ) ]
47- struct ProposalsResponse {
48- proposals : Vec < String > ,
38+ #[ serde( untagged) ]
39+ enum ProposalItem {
40+ StringFormat ( String ) ,
41+ ObjectFormat ( ProposalObject ) ,
4942}
5043
5144#[ derive( Deserialize , Debug ) ]
52- struct ReviseResponse {
53- proposal : String ,
45+ struct ProposalsResponse {
46+ proposals : Vec < ProposalItem > ,
5447}
5548
5649pub async fn generate_proposals (
@@ -72,7 +65,19 @@ pub async fn generate_proposals(
7265 if let Some ( json_start) = response_str. find ( "{" ) {
7366 let json_str = & response_str[ json_start..] ;
7467 match serde_json:: from_str :: < ProposalsResponse > ( json_str) {
75- Ok ( response) => Ok ( response. proposals ) ,
68+ Ok ( response) => {
69+ let proposals = response
70+ . proposals
71+ . into_iter ( )
72+ . map ( |item| match item {
73+ ProposalItem :: StringFormat ( s) => s,
74+ ProposalItem :: ObjectFormat ( obj) => {
75+ format ! ( "{} - {}" , obj. context, obj. question)
76+ }
77+ } )
78+ . collect ( ) ;
79+ Ok ( proposals)
80+ }
7681 Err ( e) => {
7782 // Debug: Write the JSON we tried to parse
7883 std:: fs:: write ( "/tmp/debug_json.txt" , json_str) . ok ( ) ;
@@ -90,26 +95,3 @@ pub async fn generate_proposals(
9095 ) )
9196 }
9297}
93-
94- pub async fn revise_proposal (
95- proposal : & str ,
96- revision : & str ,
97- endpoint : & str ,
98- model : & str ,
99- ) -> Result < String , anyhow:: Error > {
100- let prompt = REVISE_PROMPT
101- . replace ( "{proposal}" , proposal)
102- . replace ( "{revision}" , revision) ;
103- let response_str = call_local_model ( endpoint, model, & prompt) . await ?;
104-
105- // Attempt to find the start of the JSON object
106- if let Some ( json_start) = response_str. find ( "{" ) {
107- let json_str = & response_str[ json_start..] ;
108- match serde_json:: from_str :: < ReviseResponse > ( json_str) {
109- Ok ( response) => Ok ( response. proposal ) ,
110- Err ( e) => Err ( anyhow:: anyhow!( "Failed to parse revision JSON: {}" , e) ) ,
111- }
112- } else {
113- Err ( anyhow:: anyhow!( "No JSON object found in model response" ) )
114- }
115- }
0 commit comments