|
1 | 1 | --- |
2 | 2 | title: Model Invoking |
3 | | -description: "" |
| 3 | +description: "Invoke your models with the Modus Models API" |
4 | 4 | --- |
| 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 |
0 commit comments