Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gemini 1.5 support #349

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open

Conversation

naman1608
Copy link
Contributor

No description provided.

@naman1608
Copy link
Contributor Author

@abi even though it is working, the code being generated by the model is not for the image being uploaded, not sure if the prompt being sent is wrong or it is a model issue, how to check that?

@abi
Copy link
Owner

abi commented May 30, 2024

I have a helper function pprompt that lets you print the prompt nicely. My guess is that the image isn’t being sent correctly and that’s why it’s producing a random web page. I’ll take a look at your code to debug.

@naman1608
Copy link
Contributor Author

I used that, but only able to check the prompt in the OpenAI format, as the pprint function takes input in that format, after converting it to the format taken by Gemini is the issue I guess

@msamylea
Copy link

msamylea commented Jul 1, 2024

I rewrote the Gemini portion of this and it's working now for me. The issue was, Gemini expects the image file to be uploaded then referenced.

This is working for me:

sync def stream_gemini_response(
messages: List[ChatCompletionMessageParam],
api_key: str,
callback: Callable[[str], Awaitable[None]],
) -> str:

model = genai.GenerativeModel("gemini-1.5-flash-latest")
genai.configure(api_key=api_key)

gemini_messages = []

for message in messages:
    if isinstance(message["content"], str):
        gemini_messages.append(message["content"])
    elif isinstance(message["content"], list):
        for content in message["content"]:
            if content["type"] == "text":
                gemini_messages.append(content["text"])
            elif content["type"] == "image_url":
                image_url = content["image_url"]["url"]
                image_data = base64.b64decode(image_url.split(",")[1])
                image = Image.open(io.BytesIO(image_data))
                gemini_messages.append(image)

try:
    response = model.generate_content(gemini_messages, stream=True)
    for chunk in response:
        if chunk.text:
            await callback(chunk.text)

    full_response = "".join(chunk.text for chunk in response)
    return full_response
except Exception as e:
    print(f"An error occurred: {str(e)}")
    return ""

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants