-
-
Notifications
You must be signed in to change notification settings - Fork 643
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(instructor): improve client handling and code modularity (#562)
- Loading branch information
Showing
4 changed files
with
135 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import anthropic | ||
import instructor | ||
|
||
from typing import overload | ||
|
||
|
||
@overload | ||
def from_anthropic( | ||
client: anthropic.Anthropic, | ||
mode: instructor.Mode = instructor.Mode.ANTHROPIC_JSON, | ||
**kwargs, | ||
) -> instructor.Instructor: ... | ||
|
||
|
||
@overload | ||
def from_anthropic( | ||
client: anthropic.AsyncAnthropic, | ||
mode: instructor.Mode = instructor.Mode.ANTHROPIC_JSON, | ||
**kwargs, | ||
) -> instructor.Instructor: ... | ||
|
||
|
||
def from_anthropic( | ||
client: anthropic.Anthropic | anthropic.AsyncAnthropic, | ||
mode: instructor.Mode = instructor.Mode.ANTHROPIC_JSON, | ||
**kwargs, | ||
) -> instructor.Instructor | instructor.AsyncInstructor: | ||
assert mode in { | ||
instructor.Mode.ANTHROPIC_JSON, | ||
instructor.Mode.ANTHROPIC_TOOLS, | ||
}, "Mode be one of {instructor.Mode.ANTHROPIC_JSON, instructor.Mode.ANTHROPIC_TOOLS}" | ||
|
||
assert isinstance( | ||
client, (anthropic.Anthropic, anthropic.AsyncAnthropic) | ||
), "Client must be an instance of anthropic.Anthropic or anthropic.AsyncAnthropic" | ||
|
||
if isinstance(client, anthropic.Anthropic): | ||
return instructor.Instructor( | ||
client=client, | ||
create=instructor.patch(create=client.messages.create, mode=mode), | ||
provider=instructor.Provider.ANTHROPIC, | ||
mode=mode, | ||
**kwargs, | ||
) | ||
|
||
else: | ||
return instructor.AsyncInstructor( | ||
client=client, | ||
create=instructor.patch(create=client.messages.create, mode=mode), | ||
provider=instructor.Provider.ANTHROPIC, | ||
mode=mode, | ||
**kwargs, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
from typing import overload | ||
|
||
import groq | ||
import instructor | ||
|
||
|
||
@overload | ||
def from_groq( | ||
client: groq.Groq, | ||
mode: instructor.Mode = instructor.Mode.TOOLS, | ||
**kwargs, | ||
) -> instructor.Instructor: ... | ||
|
||
|
||
@overload | ||
def from_groq( | ||
client: groq.Groq, | ||
mode: instructor.Mode = instructor.Mode.TOOLS, | ||
**kwargs, | ||
) -> instructor.Instructor: ... | ||
|
||
|
||
def from_groq( | ||
client: groq.Groq, | ||
mode: instructor.Mode = instructor.Mode.TOOLS, | ||
**kwargs, | ||
) -> instructor.Instructor: | ||
assert mode in { | ||
instructor.Mode.JSON, | ||
instructor.Mode.TOOLS, | ||
}, "Mode be one of {instructor.Mode.JSON, instructor.Mode.TOOLS}" | ||
|
||
assert isinstance(client, (groq.Groq)), "Client must be an instance of groq.GROQ" | ||
|
||
if isinstance(client, groq.Groq): | ||
return instructor.Instructor( | ||
client=client, | ||
create=instructor.patch(create=client.chat.completions.create, mode=mode), | ||
provider=instructor.Provider.GROQ, | ||
mode=mode, | ||
**kwargs, | ||
) | ||
|
||
else: | ||
return instructor.Instructor( | ||
client=client, | ||
create=instructor.patch(create=client.messages.create, mode=mode), | ||
provider=instructor.Provider.GROQ, | ||
mode=mode, | ||
**kwargs, | ||
) |