Skip to content

Commit cf2ce22

Browse files
finish first draft for model-invoking, fix stuff
1 parent d5f689d commit cf2ce22

File tree

3 files changed

+187
-11
lines changed

3 files changed

+187
-11
lines changed

modus/app-manifest.mdx

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,8 @@ The `models` object in the app manifest allows you to easily define models, whet
242242
```json hypermode.json
243243
{
244244
"models": {
245-
"sentiment-classifier": {
246-
"sourceModel": "distilbert/distilbert-base-uncased-finetuned-sst-2-english",
245+
"text-generator": {
246+
"sourceModel": "meta-llama/Llama-3.1-8B-Instruct",
247247
"provider": "hugging-face",
248248
"host": "hypermode"
249249
}
@@ -259,11 +259,6 @@ Each model requires a unique name, specified as a key, containing only alphanume
259259
Original relative path of the model within the provider's repository.
260260
</ResponseField>
261261

262-
<ResponseField name="provider" type="string" required>
263-
Organization or directory that provided the source model, such as
264-
`hugging-face` or `openai`.
265-
</ResponseField>
266-
267262
<ResponseField name="host" type="string" required>
268263
Host for the model instance.
269264

@@ -272,8 +267,16 @@ Each model requires a unique name, specified as a key, containing only alphanume
272267

273268
</ResponseField>
274269

275-
<ResponseField name="dedicated" type="boolean" optional>
276-
Specify `yes` to use a dedicated instance of the model for your project.
270+
<ResponseField name="provider" type="string">
271+
Source provider of the model. Only `"hugging-face"` is supported for now.
272+
</ResponseField>
273+
274+
<ResponseField name="path" type="string">
275+
Path to the model endpoint, applied to the `baseUrl` of the host.
276+
</ResponseField>
277+
278+
<ResponseField name="dedicated" type="boolean">
279+
Set to `true` to use a dedicated instance of the model for your project.
277280
Defaults to `false`.
278281
</ResponseField>
279282

modus/model-invoking.mdx

Lines changed: 174 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,177 @@
11
---
22
title: Model Invoking
3-
description: ""
3+
description: "Invoke your models with the Modus Models API"
44
---
5+
6+
Modus enables you to easily integrate AI models into your app. In just a few steps, you can generate text,
7+
classify items, compute embeddings, and many more using the Models API.
8+
9+
## Prerequisites
10+
11+
Ensure that you have a working Modus setup locally.
12+
13+
## Understanding key components
14+
15+
- **Models**: You can invoke **models** hosted on Hypermode, OpenAI, Anthropic, and many more.
16+
- **Models API**: The **Models API** provides a set of host functions that you can import and call from your Modus functions.
17+
18+
## Define your models
19+
20+
You can define models in the [manifest](./app-manifest#models), here are some examples.
21+
22+
### 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
23+
24+
```json hypermode.json
25+
{
26+
...
27+
"models": {
28+
"text-generator": {
29+
"sourceModel": "meta-llama/Llama-3.1-8B-Instruct", // Model name on the provider
30+
"provider": "hugging-face", // Provider for this model
31+
"host": "hypermode" // Host where the model is running
32+
}
33+
}
34+
...
35+
}
36+
```
37+
38+
### Example 2: OpenAI's [GPT-4o](https://platform.openai.com/docs/models/gpt-4o)
39+
40+
```json hypermode.json
41+
{
42+
...
43+
"models": {
44+
"text-generator": {
45+
"sourceModel": "gpt-4o",
46+
"host": "openai",
47+
"path": "v1/chat/completions"
48+
}
49+
},
50+
// For externally hosted models, you need to define the host
51+
"hosts": {
52+
"openai": {
53+
"baseUrl": "https://api.openai.com/",
54+
"headers": {
55+
"Authorization": "Bearer {{API_KEY}}"
56+
}
57+
}
58+
}
59+
...
60+
}
61+
```
62+
63+
### Example 3: Anthropic's [Claude 3.5 Sonnet](https://docs.anthropic.com/en/docs/about-claude/models#model-comparison-table)
64+
65+
```json hypermode.json
66+
{
67+
...
68+
"models": {
69+
"text-generator": {
70+
"sourceModel": "claude-3-5-sonnet-20240620",
71+
"host": "anthropic",
72+
"path": "v1/messages"
73+
}
74+
},
75+
// For externally hosted models, you need to define the host
76+
"hosts": {
77+
"anthropic": {
78+
"baseUrl": "https://api.anthropic.com/",
79+
"headers": {
80+
"x-api-key": "{{API_KEY}}",
81+
"anthropic-version": "2023-06-01"
82+
}
83+
},
84+
}
85+
...
86+
}
87+
```
88+
89+
## Invoking a model for inference
90+
91+
To invoke a model, you need to import the Models API from the SDK.
92+
93+
### LLMs
94+
95+
Currently, the Models API support the OpenAI, Anthropic, and Gemini interface. Let's see
96+
how to invoke a model using the OpenAI interface. You can see https://platform.openai.com/docs/api-reference/chat/create for more details
97+
about the options available on the model, which you can set on the input object.
98+
99+
<Note>
100+
Hypermode-hosted LLMs implements the OpenAI API, so to use that, you can use
101+
the OpenAI interface.
102+
</Note>
103+
104+
<CodeGroup>
105+
106+
```ts AssemblyScript
107+
import { models } from "@hypermode/modus-sdk-as";
108+
import {
109+
OpenAIChatModel,
110+
ResponseFormat,
111+
SystemMessage,
112+
UserMessage,
113+
} from "@hypermode/modus-sdk-as/models/openai/chat";
114+
115+
// This model name should match the one defined in the hypermode.json manifest.
116+
const modelName: string = "text-generator";
117+
118+
export function generateText(instruction: string, prompt: string): string {
119+
const model = models.getModel<OpenAIChatModel>(modelName);
120+
const input = model.createInput([
121+
new SystemMessage(instruction),
122+
new UserMessage(prompt),
123+
]);
124+
125+
// This is one of many optional parameters available for the OpenAI Chat model.
126+
input.temperature = 0.7;
127+
128+
const output = model.invoke(input);
129+
return output.choices[0].message.content.trim();
130+
}
131+
```
132+
133+
```go Go
134+
package main
135+
136+
import (
137+
"encoding/json"
138+
"fmt"
139+
"strings"
140+
141+
"github.com/hypermodeinc/modus/sdk/go/pkg/models"
142+
"github.com/hypermodeinc/modus/sdk/go/pkg/models/openai"
143+
)
144+
145+
// This model name should match the one defined in the hypermode.json manifest file.
146+
const modelName = "text-generator"
147+
148+
func GenerateText(instruction, prompt string) (string, error) {
149+
model, err := models.GetModel[openai.ChatModel](modelName)
150+
if err != nil {
151+
return "", err
152+
}
153+
154+
input, err := model.CreateInput(
155+
openai.NewSystemMessage(instruction),
156+
openai.NewUserMessage(prompt),
157+
)
158+
if err != nil {
159+
return "", err
160+
}
161+
162+
// This is one of many optional parameters available for the OpenAI Chat model.
163+
input.Temperature = 0.7
164+
165+
output, err := model.Invoke(input)
166+
if err != nil {
167+
return "", err
168+
}
169+
return strings.TrimSpace(output.Choices[0].Message.Content), nil
170+
}
171+
```
172+
173+
</CodeGroup>
174+
175+
### Classification models
176+
177+
TODO

modus/sdk/models.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ To begin, import the `models` namespace from the SDK.
4444
<CodeGroup>
4545

4646
```ts AssemblyScript
47-
import { models } from "@hypermode/functions-as";
47+
import { models } from "@hypermode/modus-sdk-as";
4848
```
4949

5050
</CodeGroup>

0 commit comments

Comments
 (0)