MCPcopy
hub / github.com/lm-sys/FastChat / gemini_api_stream_iter

Function gemini_api_stream_iter

fastchat/serve/api_provider.py:180–233  ·  view source on GitHub ↗
(
    model_name, conv, temperature, top_p, max_new_tokens, api_key=None
)

Source from the content-addressed store, hash-verified

178
179
180def gemini_api_stream_iter(
181 model_name, conv, temperature, top_p, max_new_tokens, api_key=None
182):
183 import google.generativeai as genai # pip install google-generativeai
184
185 if api_key is None:
186 api_key = os.environ["GEMINI_API_KEY"]
187 genai.configure(api_key=api_key)
188
189 generation_config = {
190 "temperature": temperature,
191 "max_output_tokens": max_new_tokens,
192 "top_p": top_p,
193 }
194 params = {
195 "model": model_name,
196 "prompt": conv,
197 }
198 params.update(generation_config)
199 logger.info(f"==== request ====\n{params}")
200
201 safety_settings = [
202 {"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_NONE"},
203 {"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_NONE"},
204 {"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_NONE"},
205 {"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_NONE"},
206 ]
207 model = genai.GenerativeModel(
208 model_name=model_name,
209 generation_config=generation_config,
210 safety_settings=safety_settings,
211 )
212 history = []
213 for role, message in conv.messages[:-2]:
214 history.append({"role": role, "parts": message})
215 convo = model.start_chat(history=history)
216 response = convo.send_message(conv.messages[-2][1], stream=True)
217
218 try:
219 text = ""
220 for chunk in response:
221 text += chunk.text
222 data = {
223 "text": text,
224 "error_code": 0,
225 }
226 yield data
227 except Exception as e:
228 logger.error(f"==== error ====\n{e}")
229 reason = chunk.candidates
230 yield {
231 "text": f"**API REQUEST ERROR** Reason: {reason}.",
232 "error_code": 1,
233 }
234
235
236def bard_api_stream_iter(model_name, conv, temperature, top_p, api_key=None):

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…