Skip to content

Drop in a screenshot and convert it to clean code (HTML/Tailwind/React/Vue)

Python

support gemini for evals

Abi Rajaac77a9bf659c
diff --git a/backend/config.py b/backend/config.py
index 24536318..f742e70f 100644
--- a/backend/config.py
+++ b/backend/config.py
@@ -8,6 +8,7 @@ NUM_VARIANTS = 2
# LLM-related
OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY", None)
ANTHROPIC_API_KEY = os.environ.get("ANTHROPIC_API_KEY", None)
+GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY", None)
OPENAI_BASE_URL = os.environ.get("OPENAI_BASE_URL", None)
# Image generation (optional)
diff --git a/backend/evals/core.py b/backend/evals/core.py
index 37b4889c..0fab5948 100644
--- a/backend/evals/core.py
+++ b/backend/evals/core.py
@@ -1,5 +1,10 @@
-from config import ANTHROPIC_API_KEY, OPENAI_API_KEY
-from llm import Llm, stream_claude_response, stream_openai_response
+from config import ANTHROPIC_API_KEY, GEMINI_API_KEY, OPENAI_API_KEY
+from llm import (
+ Llm,
+ stream_claude_response,
+ stream_gemini_response,
+ stream_openai_response,
+)
from prompts import assemble_prompt
from prompts.types import Stack
from openai.types.chat import ChatCompletionMessageParam
@@ -31,6 +36,16 @@ async def generate_code_core(
callback=lambda x: process_chunk(x),
model=model,
)
+ elif model == Llm.GEMINI_2_0_FLASH_EXP:
+ if not GEMINI_API_KEY:
+ raise Exception("Gemini API key not found")
+
+ completion = await stream_gemini_response(
+ prompt_messages,
+ api_key=GEMINI_API_KEY,
+ callback=lambda x: process_chunk(x),
+ model=model,
+ )
else:
if not OPENAI_API_KEY:
raise Exception("OpenAI API key not found")
diff --git a/backend/llm.py b/backend/llm.py
index 06dde926..c31f5077 100644
--- a/backend/llm.py
+++ b/backend/llm.py
@@ -23,6 +23,7 @@ class Llm(Enum):
CLAUDE_3_HAIKU = "claude-3-haiku-20240307"
CLAUDE_3_5_SONNET_2024_06_20 = "claude-3-5-sonnet-20240620"
CLAUDE_3_5_SONNET_2024_10_22 = "claude-3-5-sonnet-20241022"
+ GEMINI_2_0_FLASH_EXP = "gemini-2.0-flash-exp"
# Will throw errors if you send a garbage string
@@ -234,3 +235,46 @@ async def stream_claude_response_native(
raise Exception("No HTML response found in AI response")
else:
return response.content[0].text
+
+
+async def stream_gemini_response(
+ messages: List[ChatCompletionMessageParam],
+ api_key: str,
+ callback: Callable[[str], Awaitable[None]],
+ model: Llm,
+) -> str:
+
+ client = AsyncOpenAI(
+ api_key=api_key,
+ base_url="https://generativelanguage.googleapis.com/v1beta/openai/",
+ )
+
+ # Base parameters
+ params = {
+ "model": model.value,
+ "messages": messages,
+ "stream": True,
+ "timeout": 600,
+ "temperature": 0.0,
+ "top_p": 0.95,
+ # "top_k": 40, # TODO: Not a valid param for openai?
+ "max_tokens": 8192,
+ }
+
+ stream = await client.chat.completions.create(**params) # type: ignore
+ full_response = ""
+ async for chunk in stream: # type: ignore
+ assert isinstance(chunk, ChatCompletionChunk)
+ if (
+ chunk.choices
+ and len(chunk.choices) > 0
+ and chunk.choices[0].delta
+ and chunk.choices[0].delta.content
+ ):
+ content = chunk.choices[0].delta.content or ""
+ full_response += content
+ await callback(content)
+
+ await client.close()
+
+ return full_response