# Source: https://docs.hypermode.com/modus/sdk/go/models.md

# Source: https://docs.hypermode.com/modus/sdk/assemblyscript/models.md

# Source: https://docs.hypermode.com/modus/sdk/go/models.md

# Source: https://docs.hypermode.com/modus/sdk/assemblyscript/models.md

# Source: https://docs.hypermode.com/modus/sdk/go/models.md

# Source: https://docs.hypermode.com/modus/sdk/assemblyscript/models.md

# Source: https://docs.hypermode.com/modus/sdk/go/models.md

# Source: https://docs.hypermode.com/modus/sdk/assemblyscript/models.md

# AI Model APIs

> Invoke AI models from your functions

export const SdkHeader = ({language, feature}) => <Note>
    <p>
      While each Modus SDK offers similar capabilities, the APIs and usage may
      vary between languages.
    </p>
    <p>
      Modus {feature} APIs documentation is available on the following pages:
    </p>
    <ul>
      {(() => {
  const languages = ["AssemblyScript", "Go"];
  const page = feature.toLowerCase().replace(/\W/g, "");
  return languages.map(lang => {
    if (lang === language) {
      return <li>
                <b>
                  {lang} {feature} APIs
                </b>
                (this page)
              </li>;
    } else {
      return <li>
                <a href={`../${lang.toLowerCase()}/${page}`}>
                  {lang} {feature} APIs
                </a>
              </li>;
    }
  });
})()}
    </ul>
  </Note>;

<SdkHeader language="AssemblyScript" feature="Models" />

The Modus Models APIs allow you to invoke AI models directly from your
functions, irrespective of the model's host.

Since many models have unique interfaces, the design of the Models APIs are
extremely flexible. A common base class forms the core of the APIs, which
extends to conform to any model's required schema.

The SDK contains both the base types and pre-defined implementations for many
commonly used models. You can either use one of the pre-defined model types, or
can create custom types for any model you like, by following the same pattern as
implemented in the pre-defined models.

<Tip>
  For your reference, several complete examples for using the Models APIs are available in
  [Model Invoking](/modus/model-invoking).

  Each example demonstrates using different types of AI models for different
  purposes. However, the Models interface isn't limited to these purposes. You can
  use it for any task that an AI model can perform.
</Tip>

## Import

To begin, import the `models` namespace from the SDK:

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

You'll also need to import one or more classes for the model you are working
with. For example:

```ts
import { OpenAIChatModel } from "@hypermode/modus-sdk-as/models/openai"
```

If you would like to request a new model, please
[open an issue](https://github.com/hypermodeinc/modus/issues). You can also send
a pull request, if you'd like to contribute a new model yourself.

## Models APIs

{/* vale Google.Headings = NO */}

The APIs in the `models` namespace are below, organized by category.

<Tip>
  We're constantly introducing new APIs through ongoing development with early
  users. Please [open an issue](https://github.com/hypermodeinc/modus/issues) if
  you have ideas on what would make Modus even more powerful for your next app!
</Tip>

### Functions

#### getModel

Get a model instance by name and type.

```ts
function getModel<T>(modelName: string): T
```

<ResponseField name="T" required>
  The type of model to return. This can be any class that extends the `Model`
  base class.
</ResponseField>

<ResponseField name="modelName" type="string" required>
  The name of the model to retrieve. This must match the name of a model defined
  in your project's manifest file.
</ResponseField>

### Types

#### Model

The base class for all models that Modus functions can invoke.

<Tip>
  If you are implementing a custom model, you should extend this class. You'll
  also need classes to represent the input and output types for your model. See
  the implementations of the pre-defined models in the Modus GitHub repository
  for examples.
</Tip>

```ts
abstract class Model<TInput, TOutput> {
  debug: bool
  info: ModelInfo
  invoke(input: TInput): TOutput
}
```

<ResponseField name="TInput" required>
  The type of the input data for the model. This can be any type, including a
  custom type defined in your project. It should match the shape of the data
  expected by the model. Usually a class.
</ResponseField>

<ResponseField name="TOutput" required>
  The type of the output data from the model. This can be any type, including a
  custom type defined in your project. It should match the shape of the data
  returned by the model. Usually a class.
</ResponseField>

<ResponseField name="debug" type="bool">
  A flag to enable debug mode for the model. When enabled, Modus automatically
  logs the full request and response data to the console. Implementations can
  also use this flag to enable additional debug logging. Defaults to `false`.
</ResponseField>

<ResponseField name="info" type="ModelInfo">
  Information about the model set by the Modus Runtime when creating the
  instance. See the [`ModelInfo`](#modelinfo) object for more information.
</ResponseField>

<ResponseField name="invoke(input)" type="method">
  Invokes the model with input data and returns the output data.
</ResponseField>

#### ModelInfo

Information about a model that's used to construct a `Model` instance. It is
also available as a property on the `Model` class.

<Info>
  This class relays information from the Modus runtime to the model
  implementation. Generally, you don't need to create `ModelInfo` instances
  directly.

  However, if you are implementing a custom model, you may wish to use a property
  from this class, such as `fullName`, for model providers that require the model
  name in the input request body.
</Info>

```ts
class ModelInfo {
  readonly name: string
  readonly fullName: string
}
```

<ResponseField name="name" type="string">
  The name of the model from the app manifest.
</ResponseField>

<ResponseField name="fullName" type="string">
  The full name or identifier of the model, as defined by the model provider.
</ResponseField>