tugaa commited on
Commit
6813e50
·
verified ·
1 Parent(s): 143d583

Create modules/result_synthesizer.py

Browse files
Files changed (1) hide show
  1. modules/result_synthesizer.py +34 -0
modules/result_synthesizer.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # modules/result_synthesizer.py
2
+ import os
3
+ from openai import AsyncOpenAI
4
+
5
+ client = AsyncOpenAI(api_key=os.getenv("OPENAI_API_KEY"))
6
+
7
+ class ResultSynthesizer:
8
+ def __init__(self):
9
+ pass
10
+
11
+ async def synthesize(self, results, output_lang):
12
+ formatted_results = self._format_results(results)
13
+
14
+ response = await client.chat.completions.create(
15
+ model="gpt-4o",
16
+ messages=[
17
+ {"role": "system", "content": f"You are a multilingual summarizer. Your job is to synthesize information into well-organized output in {output_lang}."},
18
+ {"role": "user", "content": formatted_results}
19
+ ]
20
+ )
21
+ return response.choices[0].message.content
22
+
23
+ def _format_results(self, results):
24
+ texts = []
25
+ for r in results:
26
+ q = r.get("query")
27
+ if "results" in r:
28
+ for res in r["results"]:
29
+ text = res.get("summary", "")
30
+ url = res.get("url", "")
31
+ texts.append(f"[Query: {q}]\n{text}\n(Source: {url})")
32
+ elif "error" in r:
33
+ texts.append(f"[Query: {q}]\nError: {r['error']}")
34
+ return "\n\n".join(texts)