Packages
nous
0.5.0
0.17.0
0.16.6
0.16.5
0.16.4
0.16.3
0.16.2
0.16.1
0.16.0
0.15.8
0.15.7
0.15.6
0.15.5
0.15.4
0.15.3
0.15.2
0.15.1
0.15.0
0.14.3
0.14.2
0.14.1
0.14.0
0.13.3
0.13.2
0.13.1
0.13.0
0.12.17
0.12.16
0.12.15
0.12.14
0.12.13
0.12.12
0.12.11
0.12.9
0.12.7
0.12.6
0.12.5
0.12.3
0.12.2
0.12.0
0.11.3
0.11.0
0.10.1
0.10.0
0.9.0
0.8.1
0.8.0
0.7.2
0.7.1
0.7.0
0.5.0
AI agent framework for Elixir with multi-provider LLM support
Current section
Files
Jump to
Current section
Files
lib/nous/model_parser.ex
defmodule Nous.ModelParser do
@moduledoc """
Parses model strings into Model configurations.
Supports the format `"provider:model-name"` and creates appropriate
Model structs with provider-specific defaults.
## Supported Formats
* `"openai:gpt-4"` - OpenAI models
* `"anthropic:claude-3-5-sonnet-20241022"` - Anthropic Claude (native API)
* `"groq:llama-3.1-70b-versatile"` - Groq models
* `"mistral:mistral-large-latest"` - Mistral models
* `"ollama:llama2"` - Local Ollama
* `"lmstudio:qwen/qwen3-30b-a3b-2507"` - Local LM Studio
* `"vllm:qwen/qwen3-30b"` - vLLM server (requires `:base_url`)
* `"openrouter:anthropic/claude-3.5-sonnet"` - OpenRouter
* `"together:meta-llama/Llama-3-70b-chat-hf"` - Together AI
* `"custom:my-model"` - Custom endpoint (requires `:base_url` option)
## Examples
# OpenAI
model = ModelParser.parse("openai:gpt-4")
# %Model{provider: :openai, model: "gpt-4", ...}
# Groq
model = ModelParser.parse("groq:llama-3.1-8b-instant")
# %Model{provider: :groq, model: "llama-3.1-8b-instant", ...}
# Custom endpoint
model = ModelParser.parse("custom:my-model",
base_url: "https://my-server.com/v1",
api_key: "my-key"
)
"""
alias Nous.Model
@doc """
Parse a model string into a Model struct.
## Parameters
* `model_string` - String in format "provider:model-name"
* `opts` - Options to override defaults (`:base_url`, `:api_key`, etc.)
## Examples
iex> %Model{provider: provider, model: model} = ModelParser.parse("openai:gpt-4")
iex> {provider, model}
{:openai, "gpt-4"}
iex> %Model{provider: provider, model: model} = ModelParser.parse("ollama:llama2")
iex> {provider, model}
{:ollama, "llama2"}
iex> model = ModelParser.parse("custom:my-model", base_url: "http://localhost:8080/v1")
iex> {model.provider, model.model, model.base_url}
{:custom, "my-model", "http://localhost:8080/v1"}
"""
@spec parse(String.t(), keyword()) :: Model.t()
def parse(model_string, opts \\ [])
def parse("openai:" <> model_name, opts) do
Model.new(:openai, model_name, opts)
end
def parse("anthropic:" <> model_name, opts) do
Model.new(:anthropic, model_name, opts)
end
def parse("gemini:" <> model_name, opts) do
Model.new(:gemini, model_name, opts)
end
def parse("groq:" <> model_name, opts) do
Model.new(:groq, model_name, opts)
end
def parse("mistral:" <> model_name, opts) do
Model.new(:mistral, model_name, opts)
end
def parse("ollama:" <> model_name, opts) do
Model.new(:ollama, model_name, opts)
end
def parse("lmstudio:" <> model_name, opts) do
Model.new(:lmstudio, model_name, opts)
end
def parse("openrouter:" <> model_name, opts) do
Model.new(:openrouter, model_name, opts)
end
def parse("together:" <> model_name, opts) do
Model.new(:together, model_name, opts)
end
def parse("vllm:" <> model_name, opts) do
unless Keyword.has_key?(opts, :base_url) do
raise ArgumentError,
"vllm provider requires :base_url option. " <>
"Example: parse(\"vllm:qwen/qwen3-30b\", base_url: \"http://localhost:8000/v1\")"
end
Model.new(:vllm, model_name, opts)
end
def parse("custom:" <> model_name, opts) do
unless Keyword.has_key?(opts, :base_url) do
raise ArgumentError,
"custom provider requires :base_url option. " <>
"Example: parse(\"custom:my-model\", base_url: \"http://localhost:8080/v1\")"
end
Model.new(:custom, model_name, opts)
end
def parse(invalid_string, _opts) do
raise ArgumentError,
"Invalid model string format: #{inspect(invalid_string)}. " <>
"Expected format: \"provider:model-name\". " <>
"Supported providers: openai, anthropic, gemini, groq, mistral, ollama, lmstudio, openrouter, together, vllm, custom"
end
end