Thanks to visit codestin.com
Credit goes to pkg.go.dev

aisdk

package module
v0.0.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 6, 2025 License: MIT Imports: 14 Imported by: 0

README

aisdk-go

GitHub Release GoDoc CI Status

[!WARNING]
This library is super new and may change a lot.

A Go implementation of Vercel's AI SDK Data Stream Protocol.

  • Supports OpenAI, Google, and Anthropic (with Bedrock support)
  • Examples for integrating useChat
  • Chain tool usage in Go, just like maxSteps
// frontend.tsx

const { messages } = useChat({
    // Points to our Go backend!
    api: "/api/chat",
})
// backend.go

// Accept the POST request...
var req *aisdk.Chat

messages, err := aisdk.MessagesToOpenAI(req.Messages)
if err != nil {
    http.Error(w, err.Error(), http.StatusInternalServerError)
    return
}

// Convert the http.ResponseWriter to a Data Stream.
dataStream := aisdk.NewDataStream(w)
stream := openaiClient.Chat.Completions.NewStreaming(...)

aisdk.PipeOpenAIToDataStream(stream, dataStream)

Development

Run tests with go test. Start the useChat demo with:

# any or all of these can be set
export OPENAI_API_KEY=<api-key>
export ANTHROPIC_API_KEY=<api-key>
export GOOGLE_API_KEY=<api-key>

cd demo
bun i
bun dev

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MessagesToAnthropic

func MessagesToAnthropic(messages []Message) ([]anthropic.MessageParam, []anthropic.TextBlockParam, error)

MessagesToAnthropic converts internal message format to Anthropic's API format. It extracts system messages into a separate slice of TextBlockParams and groups consecutive user/tool and assistant messages according to Anthropic's rules. It handles the case where a single assistant message part contains both the tool call and its result, splitting them into the required assistant tool_use and user tool_result blocks.

func MessagesToGoogle

func MessagesToGoogle(messages []Message) ([]*genai.Content, error)

MessagesToGoogle converts internal message format to Google's genai.Content slice. System messages are ignored.

func MessagesToOpenAI

func MessagesToOpenAI(messages []Message) ([]openai.ChatCompletionMessageParamUnion, error)

MessagesToOpenAI converts internal message format to OpenAI's API format.

func ToolsToAnthropic

func ToolsToAnthropic(tools []Tool) []anthropic.ToolUnionParam

ToolsToAnthropic converts the tool format to Anthropic's API format.

func ToolsToGoogle

func ToolsToGoogle(tools []Tool) ([]*genai.Tool, error)

func ToolsToOpenAI

func ToolsToOpenAI(tools []Tool) []openai.ChatCompletionToolParam

ToolsToOpenAI converts the tool format to OpenAI's API format.

func WriteDataStreamHeaders

func WriteDataStreamHeaders(w http.ResponseWriter)

Types

type Attachment

type Attachment struct {
	Name        string `json:"name,omitempty"`
	ContentType string `json:"contentType,omitempty"`
	URL         string `json:"url"`
}

type Chat

type Chat struct {
	ID       string    `json:"id"`
	Messages []Message `json:"messages"`
}

Chat is the structure sent from `useChat` to the server. This can be extended if you'd like to send additional data with `body`.

type DataStream

type DataStream iter.Seq2[DataStreamPart, error]

DataStream is a stream of DataStreamParts.

func AnthropicToDataStream

func AnthropicToDataStream(stream *ssestream.Stream[anthropic.MessageStreamEventUnion]) DataStream

AnthropicToDataStream pipes an Anthropic stream to a DataStream.

func GoogleToDataStream

func GoogleToDataStream(stream iter.Seq2[*genai.GenerateContentResponse, error]) DataStream

GoogleToDataStream pipes a Google AI stream to a DataStream.

func OpenAIToDataStream

func OpenAIToDataStream(stream *ssestream.Stream[openai.ChatCompletionChunk]) DataStream

OpenAIToDataStream pipes an OpenAI stream to a DataStream.

func (DataStream) Pipe

func (s DataStream) Pipe(w io.Writer) error

Pipe iterates over the DataStream and writes the parts to the writer.

func (DataStream) WithAccumulator

func (s DataStream) WithAccumulator(accumulator *DataStreamAccumulator) DataStream

WithAccumulator passes parts to the accumulator which aggregates them into a single message.

func (DataStream) WithToolCalling

func (s DataStream) WithToolCalling(handleToolCall func(toolCall ToolCall) any) DataStream

WithToolCalling passes tool calls to the handleToolCall function.

type DataStreamAccumulator

type DataStreamAccumulator struct {
	// contains filtered or unexported fields
}

DataStreamAccumulator accumulates DataStreamParts into Messages.

func (*DataStreamAccumulator) FinishReason

func (a *DataStreamAccumulator) FinishReason() FinishReason

func (*DataStreamAccumulator) Messages

func (a *DataStreamAccumulator) Messages() []Message

func (*DataStreamAccumulator) Push

func (*DataStreamAccumulator) Usage

func (a *DataStreamAccumulator) Usage() Usage

type DataStreamDataPart

type DataStreamDataPart struct {
	Content []any
}

DataStreamDataPart corresponds to TYPE_ID '2'.

func (DataStreamDataPart) Format

func (p DataStreamDataPart) Format() (string, error)

func (DataStreamDataPart) TypeID

func (p DataStreamDataPart) TypeID() byte

type DataStreamPart

type DataStreamPart interface {
	Format() (string, error)
	TypeID() byte
}

DataStreamPart represents a part of the Vercel AI SDK data stream.

type ErrorStreamPart

type ErrorStreamPart struct {
	Content string
}

ErrorStreamPart corresponds to TYPE_ID '3'.

func (ErrorStreamPart) Format

func (p ErrorStreamPart) Format() (string, error)

func (ErrorStreamPart) TypeID

func (p ErrorStreamPart) TypeID() byte

type FileStreamPart

type FileStreamPart struct {
	Data     []byte `json:"data"`
	MimeType string `json:"mimeType"`
}

FileStreamPart corresponds to TYPE_ID 'k'.

func (FileStreamPart) Format

func (p FileStreamPart) Format() (string, error)

func (FileStreamPart) TypeID

func (p FileStreamPart) TypeID() byte

type FinishMessageStreamPart

type FinishMessageStreamPart struct {
	FinishReason FinishReason `json:"finishReason"`
	Usage        Usage        `json:"usage"`
}

FinishMessageStreamPart corresponds to TYPE_ID 'd'.

func (FinishMessageStreamPart) Format

func (p FinishMessageStreamPart) Format() (string, error)

func (FinishMessageStreamPart) TypeID

func (p FinishMessageStreamPart) TypeID() byte

type FinishReason

type FinishReason string

FinishReason defines the possible reasons for finishing a step or message.

const (
	FinishReasonStop          FinishReason = "stop"
	FinishReasonLength        FinishReason = "length"
	FinishReasonContentFilter FinishReason = "content-filter"
	FinishReasonToolCalls     FinishReason = "tool-calls"
	FinishReasonError         FinishReason = "error"
	FinishReasonOther         FinishReason = "other"
	FinishReasonUnknown       FinishReason = "unknown"
)

type FinishStepStreamPart

type FinishStepStreamPart struct {
	FinishReason FinishReason `json:"finishReason"`
	Usage        Usage        `json:"usage"`
	IsContinued  bool         `json:"isContinued"`
}

FinishStepStreamPart corresponds to TYPE_ID 'e'.

func (FinishStepStreamPart) Format

func (p FinishStepStreamPart) Format() (string, error)

func (FinishStepStreamPart) TypeID

func (p FinishStepStreamPart) TypeID() byte

type GoogleStreamIterator

type GoogleStreamIterator interface {
	Next() (*genai.GenerateContentResponse, error)
}

GoogleStreamIterator defines the interface for iterating over Google AI stream responses. This allows for mocking in tests.

type Message

type Message struct {
	ID          string           `json:"id"`
	CreatedAt   *json.RawMessage `json:"createdAt,omitempty"`
	Content     string           `json:"content"`
	Role        string           `json:"role"`
	Parts       []Part           `json:"parts,omitempty"`
	Annotations []any            `json:"annotations,omitempty"`
	Attachments []Attachment     `json:"experimental_attachments,omitempty"`
}

type MessageAnnotationStreamPart

type MessageAnnotationStreamPart struct {
	Content []any
}

MessageAnnotationStreamPart corresponds to TYPE_ID '8'.

func (MessageAnnotationStreamPart) Format

func (MessageAnnotationStreamPart) TypeID

func (p MessageAnnotationStreamPart) TypeID() byte

type Part

type Part struct {
	Type PartType `json:"type"`

	// Type: "text"
	Text string `json:"text,omitempty"`

	// Type: "reasoning"
	Reasoning string            `json:"reasoning,omitempty"`
	Details   []ReasoningDetail `json:"details,omitempty"`

	// Type: "tool-invocation"
	ToolInvocation *ToolInvocation `json:"toolInvocation,omitempty"`

	// Type: "source"
	Source *SourceInfo `json:"source,omitempty"`

	// Type: "file"
	MimeType string `json:"mimeType,omitempty"`
	Data     []byte `json:"data,omitempty"`
	// contains filtered or unexported fields
}

type PartType

type PartType string
const (
	PartTypeText           PartType = "text"
	PartTypeReasoning      PartType = "reasoning"
	PartTypeToolInvocation PartType = "tool-invocation"
	PartTypeSource         PartType = "source"
	PartTypeFile           PartType = "file"
	PartTypeStepStart      PartType = "step-start"
)

type ReasoningDetail

type ReasoningDetail struct {
	Type      string `json:"type"`
	Text      string `json:"text,omitempty"`
	Signature string `json:"signature,omitempty"`
	Data      string `json:"data,omitempty"`
}

type ReasoningSignatureStreamPart

type ReasoningSignatureStreamPart struct {
	Signature string `json:"signature"`
}

ReasoningSignatureStreamPart corresponds to TYPE_ID 'j'.

func (ReasoningSignatureStreamPart) Format

func (ReasoningSignatureStreamPart) TypeID

type ReasoningStreamPart

type ReasoningStreamPart struct {
	Content string
}

ReasoningStreamPart corresponds to TYPE_ID 'g'.

func (ReasoningStreamPart) Format

func (p ReasoningStreamPart) Format() (string, error)

func (ReasoningStreamPart) TypeID

func (p ReasoningStreamPart) TypeID() byte

type RedactedReasoningStreamPart

type RedactedReasoningStreamPart struct {
	Data string `json:"data"`
}

RedactedReasoningStreamPart corresponds to TYPE_ID 'i'.

func (RedactedReasoningStreamPart) Format

func (RedactedReasoningStreamPart) TypeID

func (p RedactedReasoningStreamPart) TypeID() byte

type Schema

type Schema struct {
	Required   []string       `json:"required"`
	Properties map[string]any `json:"properties"`
}

type SourceInfo

type SourceInfo struct {
	URI         string         `json:"uri,omitempty"`
	ContentType string         `json:"contentType,omitempty"`
	Data        string         `json:"data,omitempty"`
	Metadata    map[string]any `json:"metadata,omitempty"`
}

type SourceStreamPart

type SourceStreamPart struct {
	SourceType string `json:"sourceType"`
	ID         string `json:"id"`
	URL        string `json:"url"`
	Title      string `json:"title"`
}

SourceStreamPart corresponds to TYPE_ID 'h'.

func (SourceStreamPart) Format

func (p SourceStreamPart) Format() (string, error)

func (SourceStreamPart) TypeID

func (p SourceStreamPart) TypeID() byte

type StartStepStreamPart

type StartStepStreamPart struct {
	MessageID string `json:"messageId"`
}

StartStepStreamPart corresponds to TYPE_ID 'f'.

func (StartStepStreamPart) Format

func (p StartStepStreamPart) Format() (string, error)

func (StartStepStreamPart) TypeID

func (p StartStepStreamPart) TypeID() byte

type TextStreamPart

type TextStreamPart struct {
	Content string
}

TextStreamPart corresponds to TYPE_ID '0'.

func (TextStreamPart) Format

func (p TextStreamPart) Format() (string, error)

func (TextStreamPart) TypeID

func (p TextStreamPart) TypeID() byte

type Tool

type Tool struct {
	Name        string `json:"name"`
	Description string `json:"description"`
	Schema      Schema `json:"parameters"`
}

type ToolCall

type ToolCall struct {
	ID   string         `json:"id"`
	Name string         `json:"name"`
	Args map[string]any `json:"args"`
}

ToolCall represents a tool call *request*.

type ToolCallDeltaStreamPart

type ToolCallDeltaStreamPart struct {
	ToolCallID    string `json:"toolCallId"`
	ArgsTextDelta string `json:"argsTextDelta"`
}

ToolCallDeltaStreamPart corresponds to TYPE_ID 'c'.

func (ToolCallDeltaStreamPart) Format

func (p ToolCallDeltaStreamPart) Format() (string, error)

func (ToolCallDeltaStreamPart) TypeID

func (p ToolCallDeltaStreamPart) TypeID() byte

type ToolCallResult

type ToolCallResult interface {
	Part | []Part | any
}

type ToolCallStartStreamPart

type ToolCallStartStreamPart struct {
	ToolCallID string `json:"toolCallId"`
	ToolName   string `json:"toolName"`
}

ToolCallStartStreamPart corresponds to TYPE_ID 'b'.

func (ToolCallStartStreamPart) Format

func (p ToolCallStartStreamPart) Format() (string, error)

func (ToolCallStartStreamPart) TypeID

func (p ToolCallStartStreamPart) TypeID() byte

type ToolCallStreamPart

type ToolCallStreamPart struct {
	ToolCallID string         `json:"toolCallId"`
	ToolName   string         `json:"toolName"`
	Args       map[string]any `json:"args"`
}

ToolCallStreamPart corresponds to TYPE_ID '9'.

func (ToolCallStreamPart) Format

func (p ToolCallStreamPart) Format() (string, error)

func (ToolCallStreamPart) TypeID

func (p ToolCallStreamPart) TypeID() byte

type ToolInvocation

type ToolInvocation struct {
	State      ToolInvocationState `json:"state"`
	Step       *int                `json:"step,omitempty"`
	ToolCallID string              `json:"toolCallId"`
	ToolName   string              `json:"toolName"`
	Args       any                 `json:"args"`
	Result     any                 `json:"result,omitempty"`
}

type ToolInvocationState

type ToolInvocationState string
const (
	ToolInvocationStateCall        ToolInvocationState = "call"
	ToolInvocationStatePartialCall ToolInvocationState = "partial-call"
	ToolInvocationStateResult      ToolInvocationState = "result"
)

type ToolResultStreamPart

type ToolResultStreamPart struct {
	ToolCallID string `json:"toolCallId"`
	Result     any    `json:"result"`
}

ToolResultStreamPart corresponds to TYPE_ID 'a'.

func (ToolResultStreamPart) Format

func (p ToolResultStreamPart) Format() (string, error)

func (ToolResultStreamPart) TypeID

func (p ToolResultStreamPart) TypeID() byte

type Usage

type Usage struct {
	PromptTokens     *int64 `json:"promptTokens"`
	CompletionTokens *int64 `json:"completionTokens"`
}

Usage details the token usage.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL