Anthropic Claude API
- 全接口字段注释
- Chatcompletion 文本对话接口
- 无缝对接官方文档:单轮问答、历史记忆问答、流返回
- 修改Prompt传参为数组模式,更方便的兼容openai的接口
- 支持swagger文档 和 ApiFox 文档
- 访问 Anthropic 官网,申请 Claude API 权限。
- 登录 Claude Console,获取 Api-Key。
- 参考下面示例使用
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")
}
}