anthropic

package module
v2.0.0-...-288f5f8 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2024 License: MIT Imports: 14 Imported by: 0

README

Anthropic-SDK-Go

Go Reference MIT Go Version GitHub release (latest by date)

GitHub Issues GitHub Repo stars GitHub repo size GitHub commit activity

Golang SDK for AnthRopic Claude AI


Features

  • Contextual sequential memory
  • Prompt automatically handles / Contextual automated processing
  • Concise and easy-to-use API
  • Fast data processing

Claude Docs: https://console.anthropic.com/docs



Start

Usage:

$ go get github.com/modeledge/anthropic-sdk-go/v2@v2.1.0

Example usage:
package main

import (
	"fmt"

	"github.com/modeledge/anthropic-sdk-go/v2"
	"github.com/modeledge/anthropic-sdk-go/v2/data"
	"github.com/modeledge/anthropic-sdk-go/v2/resp"
)

func main() {
	c, err := anthropic.New(&anthropic.Config{Key: "your keys", DefaultModel: data.ModelFullInstant})
	if err != nil {
		panic(err)
	}

	d, err := c.Send(&anthropic.Sender{
		Message: data.MessageModule{
			Human: "Do you know Golang, please answer me in the shortest possible way.",
		},
		Sender: &resp.Sender{MaxToken: 1200},
	})

	if err != nil {
		panic(err)
	}

	fmt.Println(d.Response.String())
}

Return:

{"detail":null,"completion":"Hello world! \nfmt.Println(\"Hello world!\")\n\nDone.","stop_reason":"stop_sequence","stop":"\n\nHuman:","log_id":"nop","exception":"","model":"claude-instant-v1.2","truncated":false}

Context Example:

package main

import (
	"fmt"

	"github.com/modeledge/anthropic-sdk-go/v2"
	"github.com/modeledge/anthropic-sdk-go/v2/resp"
	"github.com/modeledge/anthropic-sdk-go/v2/data"
)

func main() {
	c, err := anthropic.New(&anthropic.Config{Key: "your keys", DefaultModel: data.ModelFullInstant})
	if err != nil {
		panic(err)
	}

	d, err := c.Send(&anthropic.Sender{
		Message: data.MessageModule{
			Human: "Do you know Golang, please answer me in the shortest possible way.",
		},
		Sender: &resp.Sender{MaxToken: 1200},
	})

	if err != nil {
		panic(err)
	}

	fmt.Println(d.Response.String())

	ds, err := c.Send(&anthropic.Sender{
		Message: data.MessageModule{
			Human: "What is its current version number?",
		},
		SessionID: d.ID,
		Sender: &resp.Sender{MaxToken: 1200},
	})

	if err != nil {
		panic(err)
	}

	fmt.Println(ds.Response.String())
}

Return:

{"detail":null,"completion":"Hello world! \nfmt.Println(\"Hello world!\")\n\nDone.","stop_reason":"stop_sequence","stop":"\n\nHuman:","log_id":"nop","exception":"","model":"claude-instant-v1","truncated":false}
{"detail":null,"completion":"1.14.4 ","stop_reason":"stop_sequence","stop":"\n\nHuman:","log_id":"nop","exception":"","model":"claude-instant-v1.2","truncated":false}

Delete the session in an ID

c, err := anthropic.New(&anthropic.Config{Key: "your keys", DefaultModel: data.Model_FullInstant_1_0})
if err != nil {
	panic(err)
}

d, err := c.Send(&anthropic.Sender{
	Message: data.MessageModule{
		Human: "Do you know Golang, please answer me in the shortest possible way.",
	},
	Sender: &resp.Sender{MaxToken: 1200},
})

if err != nil {
	panic(err)
}

c.CloseSession(d)

Contribute

Move => CONTRIBUTING

Contact

Organize EMAIL: admin#zxda.top [# => @]


License

This software is distributed under MIT license.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewPool

func NewPool(c *Config) sync.Pool

Create a new Client object with a sync.Pool.

Types

type Client

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

func New

func New(c *Config) (*Client, error)

Create a new Client object.

func (*Client) Acquire

func (c *Client) Acquire() (*fasthttp.Request, *fasthttp.Response)

Acquire returns an empty fasthttp instance from request pool.

The returned fasthttp instance may be passed to Release when it is no longer needed. This allows Request recycling, reduces GC pressure and usually improves performance.

func (*Client) AcquireWithURI

func (c *Client) AcquireWithURI(url string) (*fasthttp.Request, *fasthttp.Response)

AcquireWithURI returns an empty fasthttp instance from request pool with a specified url.

The returned fasthttp instance may be passed to Release when it is no longer needed. This allows Request recycling, reduces GC pressure and usually improves performance.

func (*Client) CloseSession

func (c *Client) CloseSession(s *pool.Session) bool

Should only be used when needed.

func (*Client) Release

func (c *Client) Release(req *fasthttp.Request, res *fasthttp.Response)

Release returns req and resp acquired via Acquire to request pool.

It is forbidden accessing req and/or its' members after returning it to request pool.

func (*Client) Send

func (c *Client) Send(sender *Sender) (*pool.Session, error)

Send data to the API endpoint. Before sending out, the data will be processed into a form that the API can recognize.

func (*Client) SetTimeOut

func (c *Client) SetTimeOut(times int)

Set the response timeout in minutes.

type Config

type Config struct {
	Key          string             // API Keys
	DefaultModel string             // Choose the default AI model
	Compress     compress.Interface // Data Compress
}

Anthropic-SDK-Go configuration

type MessagesSender

type MessagesSender struct {
	Message   data.MessageArray // Chunked message structure
	SessionID string            // Session ID. If empty, a new session is automatically created. If not empty, an attempt is made to find an existing session.
	Sender    *messages.Sender
}

func NewMessagesSender

func NewMessagesSender() *MessagesSender

func (*MessagesSender) Complete

func (s *MessagesSender) Complete(client *Client, session *pool.Session) error

CompleteMessage makes a processed request to the Message API endpoint.

type Sender

type Sender struct {
	Message   data.MessageModule // Chunked message structure
	SessionID string             // Session ID. If empty, a new session is automatically created. If not empty, an attempt is made to find an existing session.
	Sender    *resp.Sender
}

func NewSender

func NewSender() *Sender

func (*Sender) Complete

func (s *Sender) Complete(client *Client, session *pool.Session) error

Make a processed request to an API endpoint.

func (*Sender) CompleteMessage

func (s *Sender) CompleteMessage(client *Client, session *pool.Session) error

CompleteMessage makes a processed request to the Message API endpoint.

Directories

Path Synopsis
pkg
pool
The pool subpackage is the new hashmap cache pool, which is only for testing now.
The pool subpackage is the new hashmap cache pool, which is only for testing now.

Jump to

Keyboard shortcuts

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