MCPcopy Create free account
hub / github.com/idank/explainshell / GeminiProvider

Class GeminiProvider

explainshell/extraction/llm/providers/gemini.py:25–189  ·  view source on GitHub ↗

Implements LLMProvider + BatchProvider for Gemini.

Source from the content-addressed store, hash-verified

23
24
25class GeminiProvider:
26 """Implements LLMProvider + BatchProvider for Gemini."""
27
28 def __init__(self, model: str, *, reasoning_effort: str | None = None) -> None:
29 self._model = model
30 self._gemini_model = model.removeprefix("gemini/")
31 self._thinking_budget: int | None = (
32 int(reasoning_effort) if reasoning_effort is not None else None
33 )
34 self.client = genai.Client(api_key=os.environ.get("GEMINI_API_KEY"))
35
36 def _thinking_config(self) -> types.ThinkingConfig | None:
37 if self._thinking_budget is None:
38 return None
39 return types.ThinkingConfig(thinking_budget=self._thinking_budget)
40
41 def call(self, user_content: str) -> tuple[str, TokenUsage]:
42 config_kwargs: dict = {
43 "system_instruction": SYSTEM_PROMPT,
44 "response_mime_type": "application/json",
45 "http_options": types.HttpOptions(timeout=LLM_TIMEOUT_SECONDS * 1000),
46 }
47 thinking = self._thinking_config()
48 if thinking:
49 config_kwargs["thinking_config"] = thinking
50 response = self.client.models.generate_content(
51 model=self._gemini_model,
52 contents=user_content,
53 config=types.GenerateContentConfig(**config_kwargs),
54 )
55 usage = TokenUsage()
56 um = response.usage_metadata
57 if um:
58 usage.input_tokens = um.prompt_token_count or 0
59 usage.output_tokens = um.candidates_token_count or 0
60 usage.reasoning_tokens = um.thoughts_token_count or 0
61 return response.text, usage
62
63 @property
64 def retryable_exceptions(self) -> tuple[type[Exception], ...]:
65 return (ClientError, ServerError, httpx.TimeoutException)
66
67 # -- Batch API --
68
69 def submit_batch(self, entries: list[BatchEntry]) -> str:
70 config_kwargs: dict = {
71 "system_instruction": SYSTEM_PROMPT,
72 "response_mime_type": "application/json",
73 }
74 thinking = self._thinking_config()
75 if thinking:
76 config_kwargs["thinking_config"] = thinking
77
78 inline_entries = []
79 for entry in entries:
80 inline_entries.append(
81 types.InlinedRequest(
82 contents=entry.user_content,

Callers 2

make_providerFunction · 0.90
make_batch_providerFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected