Skip to content

Update and add AssemblyScript code in docs #152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
6 changes: 3 additions & 3 deletions modus/data-fetching.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,9 @@ export function getPerson(name: string): Person {
const vars = new dgraph.Variables()
vars.set("$name", name)

const resp = dgraph.execute(
const resp = dgraph.executeQuery(
connection,
new dgraph.Request(new dgraph.Query(statement, vars)),
new dgraph.Query(statement, vars),
)
const persons = JSON.parse<GetPersonResponse>(resp.Json).persons
return persons[0]
Expand Down Expand Up @@ -468,7 +468,7 @@ class Person {
}
@json
class GetPersonResponse {
getPerson: Person | null
getPerson: Person | null = null
}

export function getPerson(name: string): Person | null {
Expand Down
220 changes: 139 additions & 81 deletions modus/first-modus-agent.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -103,118 +103,176 @@ AssemblyScript if you prefer. For AssemblyScript usage, refer to the
<Step title="Create your first function">
Create a function that fetches data from an external API and uses AI for analysis:

<Tab title="Go">
Create `intelligence.go`:

```go intelligence.go
package main

import (
"errors"
"fmt"
"strings"

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

type IntelReport struct {
Quote string `json:"quote"`
Author string `json:"author"`
Analysis string `json:"analysis,omitempty"`
}
<Tabs>
<Tab title="Go">
Create `intelligence.go`:

```go intelligence.go
package main

const modelName = "text-generator"
import (
"errors"
"fmt"
"strings"

// Fetch a random quote and provide AI analysis
func GatherIntelligence() (*IntelReport, error) {
request := http.NewRequest("https://zenquotes.io/api/random")
"github.com/hypermodeinc/modus/sdk/go/pkg/http"
"github.com/hypermodeinc/modus/sdk/go/pkg/models"
"github.com/hypermodeinc/modus/sdk/go/pkg/models/openai"
)

response, err := http.Fetch(request)
if err != nil {
return nil, err
type IntelReport struct {
Quote string `json:"quote"`
Author string `json:"author"`
Analysis string `json:"analysis,omitempty"`
}
if !response.Ok() {
return nil, fmt.Errorf("request failed: %d %s", response.Status, response.StatusText)

const modelName = "text-generator"

// Fetch a random quote and provide AI analysis
func GatherIntelligence() (*IntelReport, error) {
request := http.NewRequest("https://zenquotes.io/api/random")

response, err := http.Fetch(request)
if err != nil {
return nil, err
}
if !response.Ok() {
return nil, fmt.Errorf("request failed: %d %s", response.Status, response.StatusText)
}

// Parse the API response
var quotes []IntelReport
response.JSON(&quotes)
if len(quotes) == 0 {
return nil, errors.New("no data received")
}

// Get the quote
intel := quotes[0]

// Generate AI analysis
analysis, err := analyzeIntelligence(intel.Quote, intel.Author)
if err != nil {
fmt.Printf("AI analysis failed for %s: %v\n", intel.Author, err)
intel.Analysis = "Analysis unavailable"
} else {
intel.Analysis = analysis
}

return &intel, nil
}

// Parse the API response
var quotes []IntelReport
response.JSON(&quotes)
if len(quotes) == 0 {
return nil, errors.New("no data received")
// Use AI to analyze the quote
func analyzeIntelligence(quote, author string) (string, error) {
model, err := models.GetModel[openai.ChatModel](modelName)
if err != nil {
return "", err
}

prompt := `You are an analyst.
Provide a brief insight that captures the core meaning
and practical application of this wisdom in 1-2 sentences.`
content := fmt.Sprintf("Quote: \"%s\" - %s", quote, author)

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

input.Temperature = 0.7

output, err := model.Invoke(input)
if err != nil {
return "", err
}

return strings.TrimSpace(output.Choices[0].Message.Content), nil
}
```
</Tab>
<Tab title="AssemblyScript">
Modify `index.ts`

// Get the quote
intel := quotes[0]
```ts
import { http, models } from "@hypermode/modus-sdk-as";
import { OpenAIChatModel, SystemMessage, UserMessage } from "@hypermode/modus-sdk-as/models/openai/chat";

// Generate AI analysis
analysis, err := analyzeIntelligence(intel.Quote, intel.Author)
if err != nil {
fmt.Printf("AI analysis failed for %s: %v\n", intel.Author, err)
intel.Analysis = "Analysis unavailable"
} else {
intel.Analysis = analysis
}
export function sayHello(name: string | null = null): string {
return `Hello, ${name || "World"}!`;
}

return &intel, nil
@json
class IntelReport {
@alias("q")
quote!: string;
@alias("a")
author!: string;
analysis!: string;
}

// Use AI to analyze the quote
func analyzeIntelligence(quote, author string) (string, error) {
model, err := models.GetModel[openai.ChatModel](modelName)
if err != nil {
return "", err
}
const modelName = "text-generator";

export function gatherIntelligence(): IntelReport {
const response = http.fetch("https://zenquotes.io/api/random");

if (response.status !== 200)
throw new Error("Request failed with status: " + response.status.toString() + " " + response.statusText);

const quotes = response.json<IntelReport[]>();

if (!quotes.length)
throw new Error("No data recieved");

const quote = quotes[0];
const analysis = analyzeIntelligence(quote.quote, quote.author);

quote.analysis = analysis;

prompt := `You are an analyst.
return quote;
}

function analyzeIntelligence(quote: string, author: string): string {
const model = models.getModel<OpenAIChatModel>(modelName);

const prompt = `You are an analyst.
Provide a brief insight that captures the core meaning
and practical application of this wisdom in 1-2 sentences.`
content := fmt.Sprintf("Quote: \"%s\" - %s", quote, author)
and practical application of this wisdom in 1-2 sentences.`;
const content = "Quote: " + quote + " - " + author;

input, err := model.CreateInput(
openai.NewSystemMessage(prompt),
openai.NewUserMessage(content),
)
if err != nil {
return "", err
}
const input = model.createInput([
new SystemMessage(prompt),
new UserMessage(content)
]);

input.Temperature = 0.7
input.temperature = 0.7;

output, err := model.Invoke(input)
if err != nil {
return "", err
}
const output = model.invoke(input);

return strings.TrimSpace(output.Choices[0].Message.Content), nil
return output.choices[0].message.content.trim();
}
```
</Tab>
</Tab>
</Tabs>

</Step>
<Step title="Test your function">
Restart your development server:

```sh
modus dev
```

Modus automatically generates a GraphQL API from your functions.
Since your function is named `GatherIntelligence()`, it becomes a GraphQL query field called `gatherIntelligence`.

Open the Modus API Explorer at `http://localhost:8686/explorer` to test your function.
The explorer is fully GraphQL-compatible, so you can issue this query:

```graphql
query {
gatherIntelligence {
quote
author
analysis
query {
gatherIntelligence {
quote
author
analysis
}
}
}
```

You'll receive a response like:
Expand Down
Loading
Loading