MCPcopy Create free account
hub / github.com/Alibaba-NLP/DeepResearch / gemini_api

Function gemini_api

WebAgent/WebWalker/src/rag_system.py:74–146  ·  view source on GitHub ↗
(ds, output_path, search=False)

Source from the content-addressed store, hash-verified

72 asyncio.run(request_model(prompts))
73
74def gemini_api(ds, output_path, search=False):
75 if GEMINI_API_KEY is None or GEMINI_BASE_URL is None:
76 print("Please set GEMINI_API_URL and GEMINI_AUTH_TOKEN environment variables.")
77 return
78 headers = {
79 'Authorization': f'Bearer {GEMINI_API_KEY}',
80 'Content-Type': 'application/json'
81 }
82 MAX_CONCURRENT = 16
83
84 async def fetch(session, url, headers, data, semaphore, query_text):
85 async with semaphore:
86 return query_text, await _fetch_with_retry(session, url, headers, data)
87
88 @retry(stop=stop_after_attempt(10), wait=wait_exponential(min=4, max=60), reraise=True)
89 async def _fetch_with_retry(session, url, headers, data):
90 async with session.post(url, headers=headers, json=data) as response:
91 if response.status != 200:
92 print(f"Error: {response.status} - {response.reason}")
93 response.raise_for_status()
94 return await response.json()
95
96 async def run_gemini_api():
97 if not os.path.exists(output_path):
98 open(output_path, "w").close()
99 with open(output_path, "r", encoding="utf-8") as f:
100 visited = [json.loads(line)["question"] for line in f]
101 data_list = []
102 for item in ds["question"]:
103 if item not in visited:
104 data_list.append(item)
105 semaphore = asyncio.Semaphore(MAX_CONCURRENT)
106 async with aiohttp.ClientSession() as session:
107 if search:
108 tasks = [
109 fetch(session, GEMINI_BASE_URL, headers, {
110 "model": "gemini-1.5-pro",
111 "contents": [
112 {"role": "user",
113 "parts":
114 [
115 {"text": query},
116 {"tools": {
117 "google_search_retrieval": {
118 "dynamic_retrieval_config": {
119 "mode": "MODE_DYNAMIC",
120 "dynamic_threshold": 0
121 }
122 }
123 }
124 }
125 ]
126 }
127 ]
128 }, semaphore, query)
129 for query in data_list
130 ]
131 else:

Callers 1

mainFunction · 0.85

Calls 2

run_gemini_apiFunction · 0.85
runMethod · 0.45

Tested by

no test coverage detected