Skip to content

ConnectAI-E/Go-Claude

Repository files navigation

CLAUDE-SDK

Anthropic Claude API

😎点击查看官方文档

功能及特点

  1. 全接口字段注释
  2. Chatcompletion 文本对话接口
  3. 无缝对接官方文档:单轮问答、历史记忆问答、流返回
  4. 修改Prompt传参为数组模式,更方便的兼容openai的接口
  5. 支持swagger文档 和 ApiFox 文档

Swagger 文档

image

使用方法

  1. 访问 Anthropic 官网,申请 Claude API 权限。
  2. 登录 Claude Console,获取 Api-Key
  3. 参考下面示例使用

示例

package main

import (
	"context"
	"fmt"
	"github.com/ConnectAI-E/go-claude/claude"
	textv1 "github.com/ConnectAI-E/go-claude/gen/go/claude/text/v1"
	"os"
)

//init client

func main() {

	ctx := context.Background()
	client, _ := claude.New(
		claude.WithApiToken(os.Getenv("TEST_API_TOKEN")),
	)

	//chat
	req := &textv1.ChatCompletionsRequest{
		Messages: []*textv1.Message{
			{
				Role:    "Human",
				Content: "hi~",
			},
		},
		Model:             "claude-1-100k",
		Temperature:       0.7,
		MaxTokensToSample: 500,
	}
	res, _ := client.ChatCompletions(ctx, req)

	fmt.Println(res.Completion) // output: Hello

}

快速上手:

Claude Completion
package main

import (
	"context"
	"fmt"
	"github.com/ConnectAI-E/go-claude/claude"
	textv1 "github.com/ConnectAI-E/go-claude/gen/go/claude/text/v1"
	"os"
)



func main() {
	ctx := context.Background()

	client, _ := claude.New(
		claude.WithApiToken(os.Getenv("TEST_API_TOKEN")),
	)

	//chat
	req := &textv1.ChatCompletionsRequest{
		Messages: []*textv1.Message{
			{
				Role:    "Human",
				Content: "hi~",
			},
		},
		Model:             "claude-1-100k",
		Temperature:       0.7,
		MaxTokensToSample: 500,
	}
	res, _ := client.ChatCompletions(ctx, req)

	fmt.Println(res.Completion) // output: Hello

}
Claude stream completion
package main

import (
	"context"
	"errors"
	"fmt"
	"github.com/ConnectAI-E/go-claude/claude"
	textv1 "github.com/ConnectAI-E/go-claude/gen/go/claude/text/v1"
	"io"
	"os"
)



func main() {
	ctx := context.Background()
	
	//init client
	client, _ := claude.New(
		claude.WithApiToken(os.Getenv("TEST_API_TOKEN")),
	)

	//chat
	req := &textv1.ChatCompletionsRequest{
		Messages: []*textv1.Message{
			{
				Role:    "Human",
				Content: "hi~",
			},
		},
		Model:             "claude-1-100k",
		Temperature:       0.7,
		MaxTokensToSample: 500,
	}

	stream, _ := client.ChatCompletionStream(ctx, req)
	defer stream.CloseSend()
	for {
		response, err := stream.Recv()
		if errors.Is(err, io.EOF) {
			break
		}
		if err != nil {
			fmt.Println(err)
			break
		}
		fmt.Printf(response.Completion + "\n") 
	}
}
Claude history stream completion
package main

import (
	"context"
	"errors"
	"fmt"
	"github.com/ConnectAI-E/go-claude/claude"
	textv1 "github.com/ConnectAI-E/go-claude/gen/go/claude/text/v1"
	"io"
	"os"
)



func main() {
	ctx := context.Background()

	//init client
	client, _ := claude.New(
		claude.WithApiToken(os.Getenv("TEST_API_TOKEN")),
	)

	//chat
	req := &textv1.ChatCompletionsRequest{
		Messages: []*textv1.Message{
			{
				Role:    "Human",
				Content: "hi~",
			},
			{
				Role:    "Assistant",
				Content: "Hello",
            },
			{
				Role:    "Human",
				Content: "How are you?",
            },
		},
		Model:             "claude-1-100k",
		Temperature:       0.7,
		MaxTokensToSample: 500,
	}

	stream, _ := client.ChatCompletionStream(ctx, req)
	defer stream.CloseSend()
	for {
		response, err := stream.Recv()
		if errors.Is(err, io.EOF) {
			break
		}
		if err != nil {
			fmt.Println(err)
			break
		}
		fmt.Printf(response.Completion + "\n")
	}
}

Releases

No releases published

Packages

No packages published

Languages