Coder Social home page Coder Social logo

claude-webapi's Introduction

claude-webapi

claude webapi sdk writen by golang.

support the latest claude-2 model.

Preparation

  • Your sessionKey

you should login into the Claude Website firstly, find the Cookie named sessionKey, and copy it's value.

  • Your orgid

you can find your orgid in the Network panel.

Quick Start

  1. install claude-webapi sdk
go get -u github.com/all-in-aigc/claude-webapi
  1. a quick demo for chatting with claude webapi
package main

import (
	"log"

	claude "github.com/all-in-aigc/claude-webapi"
)

// custom params
var (
	baseUri    string = "https://claude.ai" // you can use a proxy address
	orgid      string = "xxx"               // your organazation id for claude account
	sessionKey string = "xxx"               // your session key after login claude.ai
	userAgent  string = "xxx"               // your request User-Agent
	debug      bool   = true                // if output request info
)

// new claude-webapi client
func getClient() *claude.Client {
	cli := claude.NewClient(
		claude.WithBaseUri(baseUri),
		claude.WithSessionKey(sessionKey),
		claude.WithOrgid(orgid),
		claude.WithUserAgent(userAgent),
		claude.WithDebug(debug),
	)

	return cli
}

func main() {
	cli := getClient()

	// chat with question
	text := "say hello to me 3 times"
	conversationId := "xxx" // your conversation id after new conversation

	params := map[string]interface{}{
		"attachments": []map[string]interface{}{},
		"completion": map[string]interface{}{
			"incremental": true,
			"model":       cli.GetModel(), // default model is "claude-2"
			"prompt":      "",
			"timezone":    "Asia/Shanghai", // your custom timezone
		},
		"organization_uuid": cli.GetOrgid(),
		"conversation_uuid": conversationId,
		"text":              text,
	}

	res, err := cli.GetChatStream(params)
	if err != nil {
		log.Fatalf("chat with prompt failed: %v", err)
	}

	reply := ""
	for v := range res.Stream {
		log.Printf("chat stream: %s\n", v.String())
		if v.Get("stop_reason").String() == "stop_sequence" {
			break
		}
		reply += v.Get("completion").String()
	}

	log.Println("reply:", reply)
}

Other use cases

  • Get Organizations
cli := getClient()

res, err := cli.GetOrganizations()

if err != nil {
	log.Fatalf("get orgs fail: %v\n", err)
}

for _, v := range res.Array() {
	orgid := v.Get("uuid").String()
	log.Printf("orgid: %s\n", orgid)
}
  • New Conversation
conversationId := uuid.New().String()
name := "my conversation"

cli := getClient()
params := map[string]interface{}{
	"name": name,
	"uuid": conversationId,
}

res, err := cli.NewConversation(params)
if err != nil {
	log.Fatalf("new conversation fail: %v\n", err)
}

conversationName := res.Get("name").String()
log.Printf("conversation name: %s\n", conversationName)
  • Get Conversations
cli := getClient()

res, err := cli.GetConversations()

if err != nil {
	log.Fatalf("get conversations fail: %v\n", err)
}

for _, v := range res.Array() {
	conversationId := v.Get("uuid").String()
	conversationName := v.Get("name").String()
	log.Printf("conversation: %s, %s\n", conversationId, conversationName)
}
  • Chat With Attachments
type ChatMessage struct {
	CreatedAt int64
	UserName  string
	Content   string
}

func getAttachment() ([]map[string]interface{}, error) {
	attachments := []map[string]interface{}{}

	msgs := []*ChatMessage{
		{1690041600, "sam", "hello"},
		{1690041605, "gpt-4", "hello my friend"},
		{1690041610, "claude", "hello boy"},
		{1690041618, "sam", "let's talk about something about llm"},
		{1690041622, "gpt-4", "sure, I'am intertesd in it"},
		{1690041625, "claude", "sounds funny"},
		{1690041631, "sam", "let's go head"},
	}

	content := "room chat messages with format: [time] user: content\n\n"
	for _, msg := range msgs {
		if msg.Content == "" {
			continue
		}

		t := time.Unix(msg.CreatedAt, 0)
		content += fmt.Sprintf("%s %s: %q\n", t.Format("2006-01-02 15:04:05"), msg.UserName, msg.Content)
	}

	if content == "" {
		return nil, errors.New("no content")
	}

	fileName := "chat_messages.txt"
	attachments = append(attachments, map[string]interface{}{
		"extracted_content": content,
		"file_name":         fileName,
		"file_size":         len(content),
		"file_type":         "text/plain",
	})

	return attachments, nil
}

func ChatWithAttachments() {
	text := `summary room chat messages in file, what're they talk about?`
	conversationId := "xxx"
	attachment, _ := getAttachment()

	cli := getClient()
	params := map[string]interface{}{
		"attachments": attachment,
		"completion": map[string]interface{}{
			"incremental": true,
			"model":       cli.GetModel(),
			"prompt":      text,
			"timezone":    "Asia/Shanghai",
		},
		"organization_uuid": cli.GetOrgid(),
		"conversation_uuid": conversationId,
		"text":              text,
	}

	res, err := cli.GetChatStream(params)

	if err != nil {
		log.Printf("get chat stream failed: %v\n", err)
	}

	reply := ""
	for v := range res.Stream {
		log.Printf("chat stream: %s\n", v.String())
		if v.Get("stop_reason").String() == "stop_sequence" {
			break
		}
		reply += v.Get("completion").String()
	}

	log.Println("reply:", reply)
}

Tests

Test cases are under the ./examples folder, you can test them by go test.

Communication

claude-webapi's People

Contributors

idoubi avatar yeatesss avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

claude-webapi's Issues

Add `convert_document` API support

Thanks so much for your stellar work here. I noticed the convert_document API isn't yet implemented when using the tool. While we can append file contents manually, that works smoothly for txt but takes a bit more effort for pdf or csv formats. Having that API would save us quite a bit of time and heavy lifting. Would it be possible to add convert_document support in a future release? That would take the tool to the next level. I appreciate you putting this together - it will be a huge help once we get convert_document up and running.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.