Skip to content

Commit

Permalink
Add Mistral AI model support (#892)
Browse files Browse the repository at this point in the history
* adding mistral.ai

* refactor: ✨ rename system.python-types to system.python_types
  • Loading branch information
pelikhan authored Nov 23, 2024
1 parent 82addda commit 66d7c0d
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 24 deletions.
49 changes: 48 additions & 1 deletion docs/src/content/docs/getting-started/configuration.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -894,9 +894,56 @@ Some models may require a Pro account.

:::

## Mistral AI <a href="" id="mistral" />

The `mistral` provider allows you to use [Mistral AI Models](https://mistral.ai/technology/#models)
using the [Mistral API](https://docs.mistral.ai/).

```js "mistral:"
script({ model: "mistral:mistral-large-latest" })
```

<Steps>

<ol>

<li>

Sign up for a [Mistral AI account](https://mistral.ai/)
and obtain an API key from their [console](https://console.mistral.ai/).

</li>

<li>

Add your Mistral AI API key to the `.env` file:

```txt title=".env"
MISTRAL_API_KEY=...
```

</li>

<li>

Update your script to use the `model` you choose.

```js
script({
...
model: "mistral:mistral-large-latest",
})
```

</li>

</ol>

</Steps>

## Alibaba Cloud <a href="" id="alibaba" />

[Alibaba Cloud](https://www.alibabacloud.com/) provides a range of AI services, including language models.
The `alibaba` provider access the [Alibaba Cloud](https://www.alibabacloud.com/) models.

```js "alibaba:"
script({
Expand Down
36 changes: 18 additions & 18 deletions docs/src/content/docs/reference/scripts/system.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2588,24 +2588,6 @@ $`You are an expert coder in Python. You create code that is PEP8 compliant.`
`````
### `system.python-types`
Python developer that adds types.
`````js wrap title="system.python-types"
system({
title: "Python developer that adds types.",
})
$`When generating Python, emit type information compatible with PyLance and Pyright.`
`````
### `system.python_code_interpreter`
Python Dockerized code execution for data analysis
Expand Down Expand Up @@ -2719,6 +2701,24 @@ defTool(
`````
### `system.python_types`
Python developer that adds types.
`````js wrap title="system.python_types"
system({
title: "Python developer that adds types.",
})
$`When generating Python, emit type information compatible with PyLance and Pyright.`
`````
### `system.retrieval_fuzz_search`
Full Text Fuzzy Search
Expand Down
16 changes: 16 additions & 0 deletions packages/core/src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import {
MODEL_PROVIDER_TRANSFORMERS,
MODEL_PROVIDER_ALIBABA,
ALIBABA_BASE,
MODEL_PROVIDER_MISTRAL,
MISTRAL_API_BASE,
} from "./constants"
import { fileExists, readText, writeText } from "./fs"
import {
Expand Down Expand Up @@ -327,6 +329,20 @@ export async function parseTokenFromEnv(
} satisfies LanguageModelConfiguration
}

if (provider === MODEL_PROVIDER_MISTRAL) {
const base = env.MISTRAL_API_BASE || MISTRAL_API_BASE
const token = env.MISTRAL_API_KEY
if (!token) throw new Error("MISTRAL_API_KEY not configured")
return {
provider,
model,
token,
base,
source: "env: MISTRAL_API_...",
type: "openai",
} satisfies LanguageModelConfiguration
}

if (provider === MODEL_PROVIDER_ALIBABA) {
const base =
env.ALIBABA_API_BASE ||
Expand Down
21 changes: 16 additions & 5 deletions packages/core/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ export const DEFAULT_MODEL_CANDIDATES = [
"azure:gpt-4o",
"azure_serverless:gpt-4o",
DEFAULT_MODEL,
"google:gemini-1.5-pro-002",
"anthropic:claude-2",
"mistral:mistral-large-latest",
"github:gpt-4o",
"client:gpt-4",
]
Expand All @@ -68,18 +70,19 @@ export const DEFAULT_VISION_MODEL_CANDIDATES = [
"azure:gpt-4o",
"azure_serverless:gpt-4o",
DEFAULT_MODEL,
"anthropic:claude-2",
"google:gemini-1.5-pro-002",
"anthropic:claude-2",
"github:gpt-4o",
]
export const DEFAULT_SMALL_MODEL = "openai:gpt-4o-mini"
export const DEFAULT_SMALL_MODEL_CANDIDATES = [
"azure:gpt-4o-mini",
"azure_serverless:gpt-4o-mini",
DEFAULT_SMALL_MODEL,
"google:gemini-1.5-flash-002",
"anthropic:claude-instant-1",
"mistral:mistral-small-latest",
"github:gpt-4o-mini",
"google:gemini-1.5-flash-002",
"client:gpt-4-mini",
]
export const DEFAULT_EMBEDDINGS_MODEL_CANDIDATES = [
Expand Down Expand Up @@ -147,8 +150,11 @@ export const LOCALAI_API_BASE = "http://localhost:8080/v1"
export const LITELLM_API_BASE = "http://localhost:4000"
export const ANTHROPIC_API_BASE = "https://api.anthropic.com"
export const HUGGINGFACE_API_BASE = "https://api-inference.huggingface.co/v1"
export const GOOGLE_API_BASE =
"https://generativelanguage.googleapis.com/v1beta/openai/"
export const ALIBABA_BASE =
"https://dashscope-intl.aliyuncs.com/compatible-mode/v1"
export const MISTRAL_API_BASE = "https://api.mistral.ai/v1"

export const PROMPTFOO_CACHE_PATH = ".genaiscript/cache/tests"
export const PROMPTFOO_CONFIG_DIR = ".genaiscript/config/tests"
Expand Down Expand Up @@ -177,6 +183,7 @@ export const MODEL_PROVIDER_ANTHROPIC = "anthropic"
export const MODEL_PROVIDER_HUGGINGFACE = "huggingface"
export const MODEL_PROVIDER_TRANSFORMERS = "transformers"
export const MODEL_PROVIDER_ALIBABA = "alibaba"
export const MODEL_PROVIDER_MISTRAL = "mistral"

export const TRACE_FILE_PREVIEW_MAX_LENGTH = 240

Expand Down Expand Up @@ -219,6 +226,8 @@ export const DOCS_CONFIGURATION_HUGGINGFACE_TRANSFORMERS_URL =
"https://microsoft.github.io/genaiscript/getting-started/configuration/#transformers"
export const DOCS_CONFIGURATION_ALIBABA_URL =
"https://microsoft.github.io/genaiscript/getting-started/configuration/#alibaba"
export const DOCS_CONFIGURATION_MISTRAL_URL =
"https://microsoft.github.io/genaiscript/getting-started/configuration/#mistral"
export const DOCS_CONFIGURATION_CONTENT_SAFETY_URL =
"https://microsoft.github.io/genaiscript/reference/scripts/content-safety"
export const DOCS_DEF_FILES_IS_EMPTY_URL =
Expand Down Expand Up @@ -271,6 +280,11 @@ export const MODEL_PROVIDERS = Object.freeze([
detail: "Hugging Face models",
url: DOCS_CONFIGURATION_HUGGINGFACE_URL,
},
{
id: MODEL_PROVIDER_MISTRAL,
detail: "Mistral AI",
url: DOCS_CONFIGURATION_MISTRAL_URL,
},
{
id: MODEL_PROVIDER_TRANSFORMERS,
detail: "Hugging Face Transformers",
Expand Down Expand Up @@ -387,8 +401,5 @@ export const CHOICE_LOGIT_BIAS = 5
export const SANITIZED_PROMPT_INJECTION =
"...prompt injection detected, content removed..."

export const GOOGLE_API_BASE =
"https://generativelanguage.googleapis.com/v1beta/openai/"

export const IMAGE_DETAIL_LOW_WIDTH = 512
export const IMAGE_DETAIL_LOW_HEIGHT = 512
20 changes: 20 additions & 0 deletions packages/core/src/pricing.json
Original file line number Diff line number Diff line change
Expand Up @@ -327,5 +327,25 @@
"alibaba:qwen-turbo": {
"price_per_million_input_tokens": 0.4,
"price_per_million_output_tokens": 1.2
},
"mistral:mistral-large-latest": {
"price_per_million_input_tokens": 2,
"price_per_million_output_tokens": 6
},
"mistral:mistral-small-latest": {
"price_per_million_input_tokens": 0.2,
"price_per_million_output_tokens": 0.6
},
"mistral:pixtral-large-latest": {
"price_per_million_input_tokens": 2,
"price_per_million_output_tokens": 6
},
"mistral:codestral-latest": {
"price_per_million_input_tokens": 0.2,
"price_per_million_output_tokens": 0.6
},
"mistral:mistral-nemo": {
"price_per_million_input_tokens": 0.2,
"price_per_million_output_tokens": 0.6
}
}
5 changes: 5 additions & 0 deletions packages/core/src/types/prompt_template.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@ type ModelType = OptionsOrString<
| "google:gemini-1.5-pro"
| "google:gemini-1.5-pro-002"
| "google:gemini-1-pro"
| "mistral:mistral-large-latest"
| "mistral:mistral-small-latest"
| "mistral:pixtral-large-latest"
| "mistral:codestral-latest"
| "mistral:nemo"
| "alibaba:qwen-turbo"
| "alibaba:qwen-max"
| "alibaba:qwen-plus"
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/usage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ import pricings from "./pricing.json" // Interface to hold statistics related to
import { parseModelIdentifier } from "./models"
import {
MODEL_PROVIDER_AICI,
MODEL_PROVIDER_ALIBABA,
MODEL_PROVIDER_ANTHROPIC,
MODEL_PROVIDER_AZURE_OPENAI,
MODEL_PROVIDER_AZURE_SERVERLESS_MODELS,
MODEL_PROVIDER_AZURE_SERVERLESS_OPENAI,
MODEL_PROVIDER_GITHUB,
MODEL_PROVIDER_GOOGLE,
MODEL_PROVIDER_MISTRAL,
MODEL_PROVIDER_OPENAI,
} from "./constants"

Expand Down Expand Up @@ -91,6 +93,8 @@ export function isCosteable(model: string) {
MODEL_PROVIDER_AZURE_SERVERLESS_OPENAI,
MODEL_PROVIDER_ANTHROPIC,
MODEL_PROVIDER_GOOGLE,
MODEL_PROVIDER_ALIBABA,
MODEL_PROVIDER_MISTRAL,
]
return costeableProviders.includes(provider)
}
Expand Down

0 comments on commit 66d7c0d

Please sign in to comment.