Skip to content
Merged
Show file tree
Hide file tree
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
28 changes: 15 additions & 13 deletions modus/app-manifest.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,8 @@
```json hypermode.json
{
"models": {
"sentiment-classifier": {
"task": "classification",
"sourceModel": "distilbert/distilbert-base-uncased-finetuned-sst-2-english",
"text-generator": {
"sourceModel": "meta-llama/Llama-3.1-8B-Instruct",
"provider": "hugging-face",
"host": "hypermode"
}
Expand All @@ -256,20 +255,10 @@

### Models Properties

<ResponseField name="task" type="string" required>
Training intent of the model. Currently, must be one of: `classification`,
`embedding`, or `generation`.
</ResponseField>

<ResponseField name="sourceModel" type="string" required>
Original relative path of the model within the provider's repository.
</ResponseField>

<ResponseField name="provider" type="string" required>
Organization or directory that provided the source model, such as
`hugging-face` or `openai`.
</ResponseField>

<ResponseField name="host" type="string" required>
Host for the model instance.

Expand All @@ -278,6 +267,19 @@

</ResponseField>

<ResponseField name="provider" type="string">
Source provider of the model. Only `"hugging-face"` is supported for now.

Check failure on line 271 in modus/app-manifest.mdx

View check run for this annotation

Trunk.io / Trunk Check

vale(error)

[new] In general, use active voice instead of passive voice ('is supported').

Check warning on line 271 in modus/app-manifest.mdx

View workflow job for this annotation

GitHub Actions / Vale Lanugage Review

[vale] reported by reviewdog 🐶 [Google.Passive] In general, use active voice instead of passive voice ('is supported'). Raw Output: {"message": "[Google.Passive] In general, use active voice instead of passive voice ('is supported').", "location": {"path": "modus/app-manifest.mdx", "range": {"start": {"line": 271, "column": 55}}}, "severity": "INFO"}
</ResponseField>

<ResponseField name="path" type="string">
Path to the model endpoint, applied to the `baseUrl` of the host.
</ResponseField>

<ResponseField name="dedicated" type="boolean">
Set to `true` to use a dedicated instance of the model for your project.
Defaults to `false`.
</ResponseField>

### Auto-deployed models

When using `hugging-face` as the `provider` and `hypermode` as the `host`, Hypermode automatically deploys a dedicated
Expand Down
175 changes: 174 additions & 1 deletion modus/model-invoking.mdx
Original file line number Diff line number Diff line change
@@ -1,4 +1,177 @@
---
title: Model Invoking
description: ""
description: "Invoke your models with the Modus Models API"
---

Modus enables you to easily integrate AI models into your app. In just a few steps, you can generate text,
classify items, compute embeddings, and many more using the Models API.

## Prerequisites

Ensure that you have a working Modus setup locally.

## Understanding key components

- **Models**: You can invoke **models** hosted on Hypermode, OpenAI, Anthropic, and many more.
- **Models API**: The **Models API** provides a set of host functions that you can import and call from your Modus functions.

## Define your models

You can define models in the [manifest](./app-manifest#models), here are some examples.

### Example 1: Meta's [Llama-3.1-8B-Instruct](https://huggingface.co/meta-llama/Llama-3.1-8B-Instruct) model from Hugging Face, hosted on Hypermode

Check failure on line 22 in modus/model-invoking.mdx

View check run for this annotation

Trunk.io / Trunk Check

vale(error)

[new] Put a nonbreaking space between the number and the unit in '8B'.

Check failure on line 22 in modus/model-invoking.mdx

View workflow job for this annotation

GitHub Actions / Vale Lanugage Review

[vale] reported by reviewdog 🐶 [Google.Units] Put a nonbreaking space between the number and the unit in '8B'. Raw Output: {"message": "[Google.Units] Put a nonbreaking space between the number and the unit in '8B'.", "location": {"path": "modus/model-invoking.mdx", "range": {"start": {"line": 22, "column": 34}}}, "severity": "ERROR"}

```json hypermode.json
{
...
"models": {
"text-generator": {
"sourceModel": "meta-llama/Llama-3.1-8B-Instruct", // Model name on the provider
"provider": "hugging-face", // Provider for this model
"host": "hypermode" // Host where the model is running
}
}
...
}
```

### Example 2: OpenAI's [GPT-4o](https://platform.openai.com/docs/models/gpt-4o)

Check failure on line 38 in modus/model-invoking.mdx

View check run for this annotation

Trunk.io / Trunk Check

vale(error)

[new] Spell out 'GPT', if it's unfamiliar to the audience.

Check failure on line 38 in modus/model-invoking.mdx

View check run for this annotation

Trunk.io / Trunk Check

vale(error)

[new] 'Example 2: OpenAI's GPT-4o' should use sentence-style capitalization.

Check warning on line 38 in modus/model-invoking.mdx

View workflow job for this annotation

GitHub Actions / Vale Lanugage Review

[vale] reported by reviewdog 🐶 [Google.Headings] 'Example 2: OpenAI's GPT-4o' should use sentence-style capitalization. Raw Output: {"message": "[Google.Headings] 'Example 2: OpenAI's GPT-4o' should use sentence-style capitalization.", "location": {"path": "modus/model-invoking.mdx", "range": {"start": {"line": 38, "column": 1}}}, "severity": "WARNING"}

Check warning on line 38 in modus/model-invoking.mdx

View workflow job for this annotation

GitHub Actions / Vale Lanugage Review

[vale] reported by reviewdog 🐶 [Google.Acronyms] Spell out 'GPT', if it's unfamiliar to the audience. Raw Output: {"message": "[Google.Acronyms] Spell out 'GPT', if it's unfamiliar to the audience.", "location": {"path": "modus/model-invoking.mdx", "range": {"start": {"line": 38, "column": 26}}}, "severity": "INFO"}

```json hypermode.json
{
...
"models": {
"text-generator": {
"sourceModel": "gpt-4o",
"host": "openai",
"path": "v1/chat/completions"
}
},
// For externally hosted models, you need to define the host
"hosts": {
"openai": {
"baseUrl": "https://api.openai.com/",
"headers": {
"Authorization": "Bearer {{API_KEY}}"
}
}
}
...
}
```

### Example 3: Anthropic's [Claude 3.5 Sonnet](https://docs.anthropic.com/en/docs/about-claude/models#model-comparison-table)

Check failure on line 63 in modus/model-invoking.mdx

View check run for this annotation

Trunk.io / Trunk Check

vale(error)

[new] 'Example 3: Anthropic's Claude 3.5 Sonnet' should use sentence-style capitalization.

Check failure on line 63 in modus/model-invoking.mdx

View check run for this annotation

Trunk.io / Trunk Check

vale(error)

[new] Did you really mean 'Anthropic's'?

Check warning on line 63 in modus/model-invoking.mdx

View workflow job for this annotation

GitHub Actions / Vale Lanugage Review

[vale] reported by reviewdog 🐶 [Google.Headings] 'Example 3: Anthropic's Claude 3.5 Sonnet' should use sentence-style capitalization. Raw Output: {"message": "[Google.Headings] 'Example 3: Anthropic's Claude 3.5 Sonnet' should use sentence-style capitalization.", "location": {"path": "modus/model-invoking.mdx", "range": {"start": {"line": 63, "column": 1}}}, "severity": "WARNING"}

Check failure on line 63 in modus/model-invoking.mdx

View workflow job for this annotation

GitHub Actions / Vale Lanugage Review

[vale] reported by reviewdog 🐶 [Vale.Spelling] Did you really mean 'Anthropic's'? Raw Output: {"message": "[Vale.Spelling] Did you really mean 'Anthropic's'?", "location": {"path": "modus/model-invoking.mdx", "range": {"start": {"line": 63, "column": 16}}}, "severity": "ERROR"}

```json hypermode.json
{
...
"models": {
"text-generator": {
"sourceModel": "claude-3-5-sonnet-20240620",
"host": "anthropic",
"path": "v1/messages"
}
},
// For externally hosted models, you need to define the host
"hosts": {
"anthropic": {
"baseUrl": "https://api.anthropic.com/",
"headers": {
"x-api-key": "{{API_KEY}}",
"anthropic-version": "2023-06-01"
}
},
}
...
}
```

## Invoking a model for inference

To invoke a model, you need to import the Models API from the SDK.

### LLMs

Check failure on line 93 in modus/model-invoking.mdx

View check run for this annotation

Trunk.io / Trunk Check

vale(error)

[new] Did you really mean 'LLMs'?

Check failure on line 93 in modus/model-invoking.mdx

View check run for this annotation

Trunk.io / Trunk Check

vale(error)

[new] 'LLMs' should use sentence-style capitalization.

Check warning on line 93 in modus/model-invoking.mdx

View workflow job for this annotation

GitHub Actions / Vale Lanugage Review

[vale] reported by reviewdog 🐶 [Google.Headings] 'LLMs' should use sentence-style capitalization. Raw Output: {"message": "[Google.Headings] 'LLMs' should use sentence-style capitalization.", "location": {"path": "modus/model-invoking.mdx", "range": {"start": {"line": 93, "column": 5}}}, "severity": "WARNING"}

Check failure on line 93 in modus/model-invoking.mdx

View workflow job for this annotation

GitHub Actions / Vale Lanugage Review

[vale] reported by reviewdog 🐶 [Vale.Spelling] Did you really mean 'LLMs'? Raw Output: {"message": "[Vale.Spelling] Did you really mean 'LLMs'?", "location": {"path": "modus/model-invoking.mdx", "range": {"start": {"line": 93, "column": 5}}}, "severity": "ERROR"}

Currently, the Models API support the OpenAI, Anthropic, and Gemini interface. Let's see
how to invoke a model using the OpenAI interface. You can see https://platform.openai.com/docs/api-reference/chat/create for more details
about the options available on the model, which you can set on the input object.

<Note>
Hypermode-hosted LLMs implements the OpenAI API, so to use that, you can use

Check failure on line 100 in modus/model-invoking.mdx

View check run for this annotation

Trunk.io / Trunk Check

vale(error)

[new] Did you really mean 'LLMs'?

Check failure on line 100 in modus/model-invoking.mdx

View workflow job for this annotation

GitHub Actions / Vale Lanugage Review

[vale] reported by reviewdog 🐶 [Vale.Spelling] Did you really mean 'LLMs'? Raw Output: {"message": "[Vale.Spelling] Did you really mean 'LLMs'?", "location": {"path": "modus/model-invoking.mdx", "range": {"start": {"line": 100, "column": 20}}}, "severity": "ERROR"}
the OpenAI interface.
</Note>

<CodeGroup>

```ts AssemblyScript
import { models } from "@hypermode/modus-sdk-as";
import {
OpenAIChatModel,
ResponseFormat,
SystemMessage,
UserMessage,
} from "@hypermode/modus-sdk-as/models/openai/chat";

// This model name should match the one defined in the hypermode.json manifest.
const modelName: string = "text-generator";

export function generateText(instruction: string, prompt: string): string {
const model = models.getModel<OpenAIChatModel>(modelName);
const input = model.createInput([
new SystemMessage(instruction),
new UserMessage(prompt),
]);

// This is one of many optional parameters available for the OpenAI Chat model.
input.temperature = 0.7;

const output = model.invoke(input);
return output.choices[0].message.content.trim();
}
```

```go Go
package main

import (
"encoding/json"
"fmt"
"strings"

"github.com/hypermodeinc/modus/sdk/go/pkg/models"
"github.com/hypermodeinc/modus/sdk/go/pkg/models/openai"
)

// This model name should match the one defined in the hypermode.json manifest file.
const modelName = "text-generator"

func GenerateText(instruction, prompt string) (string, error) {
model, err := models.GetModel[openai.ChatModel](modelName)
if err != nil {
return "", err
}

input, err := model.CreateInput(
openai.NewSystemMessage(instruction),
openai.NewUserMessage(prompt),
)
if err != nil {
return "", err
}

// This is one of many optional parameters available for the OpenAI Chat model.
input.Temperature = 0.7

output, err := model.Invoke(input)
if err != nil {
return "", err
}
return strings.TrimSpace(output.Choices[0].Message.Content), nil
}
```

</CodeGroup>

### Classification models

TODO
2 changes: 1 addition & 1 deletion modus/sdk/models.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ To begin, import the `models` namespace from the SDK.
<CodeGroup>

```ts AssemblyScript
import { models } from "@hypermode/functions-as";
import { models } from "@hypermode/modus-sdk-as";
```

</CodeGroup>
Expand Down
Loading