Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions go/ai/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ func (p *Part) MarshalJSON() ([]byte, error) {
// Part is defined in TypeScript as a union.

if p.isText {
v := TextPart{
v := textPart{
Text: p.text,
}
return json.Marshal(v)
} else {
v := MediaPart{
Media: &MediaPartMedia{
v := mediaPart{
Media: &mediaPartMedia{
ContentType: p.contentType,
Url: p.text,
Url: p.text,
},
}
return json.Marshal(v)
Expand All @@ -88,8 +88,8 @@ func (p *Part) UnmarshalJSON(b []byte) error {
// Part is defined in TypeScript as a union.

var s struct {
Text string `json:"text,omitempty"`
Media *MediaPartMedia `json:"media,omitempty"`
Text string `json:"text,omitempty"`
Media *mediaPartMedia `json:"media,omitempty"`
}

if err := json.Unmarshal(b, &s); err != nil {
Expand Down
22 changes: 18 additions & 4 deletions go/ai/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

package ai

// A Candidate is one of several possible generated responses from a generation
// request. It contains a single generated message along with additional
// metadata about its generation. A generation request may result in multiple Candidates.
type Candidate struct {
Custom any `json:"custom,omitempty"`
FinishMessage string `json:"finishMessage,omitempty"`
Expand All @@ -25,6 +28,7 @@ type Candidate struct {
Usage *GenerationUsage `json:"usage,omitempty"`
}

// FinishReason is the reason why a model stopped generating tokens.
type FinishReason string

const (
Expand All @@ -35,6 +39,7 @@ const (
FinishReasonUnknown FinishReason = "unknown"
)

// A GenerateRequest is a request to generate completions from a model.
type GenerateRequest struct {
Candidates int `json:"candidates,omitempty"`
Config any `json:"config,omitempty"`
Expand All @@ -45,25 +50,31 @@ type GenerateRequest struct {
Tools []*ToolDefinition `json:"tools,omitempty"`
}

// GenerateRequestOutput describes the structure that the model's output
// should conform to. If Format is [OutputFormatJSON], then Schema
// can describe the desired form of the generated JSON.
type GenerateRequestOutput struct {
Format OutputFormat `json:"format,omitempty"`
Schema map[string]any `json:"schema,omitempty"`
}

// OutputFormat is the format that the model's output should produce.
type OutputFormat string

const (
OutputFormatJSON OutputFormat = "json"
OutputFormatText OutputFormat = "text"
)

// A GenerateResponse is a model's response to a [GenerateRequest].
type GenerateResponse struct {
Candidates []*Candidate `json:"candidates,omitempty"`
Custom any `json:"custom,omitempty"`
Request *GenerateRequest `json:"request,omitempty"`
Usage *GenerationUsage `json:"usage,omitempty"`
}

// GenerationCommonConfig holds configuration for generation.
type GenerationCommonConfig struct {
MaxOutputTokens int `json:"maxOutputTokens,omitempty"`
StopSequences []string `json:"stopSequences,omitempty"`
Expand All @@ -73,22 +84,24 @@ type GenerationCommonConfig struct {
Version string `json:"version,omitempty"`
}

// GenerationUsage provides information about the generation process.
type GenerationUsage struct {
Custom map[string]float64 `json:"custom,omitempty"`
InputTokens float64 `json:"inputTokens,omitempty"`
OutputTokens float64 `json:"outputTokens,omitempty"`
TotalTokens float64 `json:"totalTokens,omitempty"`
}

type MediaPart struct {
Media *MediaPartMedia `json:"media,omitempty"`
type mediaPart struct {
Media *mediaPartMedia `json:"media,omitempty"`
}

type MediaPartMedia struct {
type mediaPartMedia struct {
ContentType string `json:"contentType,omitempty"`
Url string `json:"url,omitempty"`
}

// Message is the contents of a model response.
type Message struct {
Content []*Part `json:"content,omitempty"`
Role Role `json:"role,omitempty"`
Expand All @@ -109,10 +122,11 @@ const (
RoleTool Role = "tool"
)

type TextPart struct {
type textPart struct {
Text string `json:"text,omitempty"`
}

// A ToolDefinition describes a tool.
type ToolDefinition struct {
// Valid JSON Schema representing the input of the tool.
InputSchema map[string]any `json:"inputSchema,omitempty"`
Expand Down
47 changes: 47 additions & 0 deletions go/genkit/schemas.config
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,21 @@ TraceData omit

Candidate.index type int
CandidateFinishReason name FinishReason
Candidate doc
A Candidate is one of several possible generated responses from a generation
request. It contains a single generated message along with additional
metadata about its generation. A generation request may result in multiple Candidates.
.

CandidateFinishReason doc
FinishReason is the reason why a model stopped generating tokens.
.

GenerateRequest.candidates type int
GenerateRequest doc
A GenerateRequest is a request to generate completions from a model.
.

GenerationCommonConfig.maxOutputTokens type int
GenerationCommonConfig.topK type int

Expand Down Expand Up @@ -76,6 +89,9 @@ GenerateRequest pkg ai
GenerateRequestOutput pkg ai
GenerateRequestOutputFormat pkg ai
GenerationUsage pkg ai
GenerationUsage doc
GenerationUsage provides information about the generation process.
.
GenerationCommonConfig pkg ai
Message pkg ai
ToolDefinition pkg ai
Expand All @@ -85,9 +101,40 @@ ToolResponsePart pkg ai
ToolResponsePartToolResponse pkg ai
Part pkg ai
TextPart pkg ai
TextPart name textPart
MediaPart pkg ai
MediaPart name mediaPart
MediaPartMedia pkg ai
MediaPartMedia name mediaPartMedia
Role pkg ai
RoleUser pkg ai
RoleModel pkg ai
RoleTool pkg ai


GenerateRequestOutput doc
GenerateRequestOutput describes the structure that the model's output
should conform to. If Format is [OutputFormatJSON], then Schema
can describe the desired form of the generated JSON.
.

GenerateRequestOutputFormat doc
OutputFormat is the format that the model's output should produce.
.

GenerateResponse doc
A GenerateResponse is a model's response to a [GenerateRequest].
.

GenerationCommonConfig doc
GenerationCommonConfig holds configuration for generation.
.

Message doc
Message is the contents of a model response.
.

ToolDefinition doc
A ToolDefinition describes a tool.
.