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

Add helper to simplify selecting ChatModel #416

Open
jackmpcollins opened this issue Feb 2, 2025 · 0 comments
Open

Add helper to simplify selecting ChatModel #416

jackmpcollins opened this issue Feb 2, 2025 · 0 comments

Comments

@jackmpcollins
Copy link
Owner

To avoid long import paths and make it easier to change model using just a string, add a model function that returns the appropriate ChatModel instance based on the name provided. This should also accept all other model args (temperature, max_tokens, etc.). For models that do not support the provided args we can raise an error, warn, or ignore, and this should be configurable by a parameter.

This function should be used for creation of the default ChatModel from env vars, to keep behavior consistent. Including giving a clear error message if the required dependency is not installed. Related issue: #415

There should be a way to register new ChatModel classes for users that create their own.

Usage

from magentic import model, prompt


@prompt("Say hello")
def say_hello() -> str: ...


@prompt(
    "Say hello",
    model=model("anthropic:claude-3-5-sonnet-latest"),
    # And even just
    model="anthropic:claude-3-5-sonnet-latest",
)
def say_hello_anthropic() -> str: ...


say_hello()  # Uses env vars or default settings

with model("openai:gpt-4o-mini", temperature=1):
    say_hello()  # Uses openai with gpt-4o-mini and temperature=1 due to context manager
    say_hello_anthropic()  # Uses Anthropic claude-3-5-sonnet-latest because explicitly configured
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

No branches or pull requests

1 participant