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

async gemini support #719

Merged
merged 4 commits into from
May 31, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 33 additions & 6 deletions instructor/client_gemini.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,55 @@
# type: ignore
from __future__ import annotations

from typing import Any, Literal, overload

import google.generativeai as genai

import instructor

from typing import Any

@overload
def from_gemini(
client: genai.GenerativeModel,
mode: instructor.Mode = instructor.Mode.GEMINI_JSON,
use_async: Literal[True] = True,
**kwargs: Any,
) -> instructor.AsyncInstructor: ...


@overload
def from_gemini(
client: genai.GenerativeModel,
mode: instructor.Mode = instructor.Mode.GEMINI_JSON,
use_async: Literal[False] = False,
**kwargs: Any,
) -> instructor.Instructor:
assert (
mode == instructor.Mode.GEMINI_JSON
), "Mode must be instructor.Mode.GEMINI_JSON"
) -> instructor.Instructor: ...


def from_gemini(
client: genai.GenerativeModel,
mode: instructor.Mode = instructor.Mode.GEMINI_JSON,
use_async: bool = False,
**kwargs: Any,
) -> instructor.Instructor | instructor.AsyncInstructor:
assert mode == instructor.Mode.GEMINI_JSON, "Mode must be instructor.Mode.GEMINI_JSON"

assert isinstance(
client,
(genai.GenerativeModel),
), "Client must be an instance of genai.generativemodel"

create = client.generate_content
if use_async:
create = client.generate_content_async
return instructor.AsyncInstructor(
client=client,
create=instructor.patch(create=create, mode=mode),
provider=instructor.Provider.GEMINI,
mode=mode,
**kwargs,
)

create = client.generate_content
return instructor.Instructor(
client=client,
create=instructor.patch(create=create, mode=mode),
Expand Down
Loading