anthropic

package module
v2.1.1 Latest Latest
Warning

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

Go to latest
Published: Mar 14, 2024 License: MIT Imports: 13 Imported by: 1

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/3JoB/anthropic-sdk-go/v2@v2.1.0

Example usage:
package main

import (
	"fmt"

	"github.com/3JoB/anthropic-sdk-go/v2"
	"github.com/3JoB/anthropic-sdk-go/v2/data"
	"github.com/3JoB/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/3JoB/anthropic-sdk-go/v2"
	"github.com/3JoB/anthropic-sdk-go/v2/resp"
	"github.com/3JoB/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 added in v2.1.0

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) CloseSession added in v2.1.0

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

Should only be used when needed.

func (*Client) Release added in v2.1.0

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 added in v2.0.2

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

Anthropic-SDK-Go configuration

type Sender added in v2.1.0

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 added in v2.1.0

func NewSender() *Sender

func (*Sender) Complete added in v2.1.0

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

Make a processed request to an 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