chatglm

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2023 License: Apache-2.0 Imports: 9 Imported by: 3

README

Go ChatGLM

Go Reference Go Report Card

This library provides unofficial Go clients for ChatGLM API. We support:

Installation:

go get github.com/sunny0826/go-chatglm

Select Model

  • ChatGLMLite
  • ChatGLMStd
  • ChatGLMPro
m := chatglm.ModelAPI{
    Model:       chatglm.ChatGLMLite,
    // Model:       chatglm.ChatGLMStd,
    // Model:       chatglm.ChatGLMPro,
}

Example

Sync
package main

import (
	"fmt"
	"github.com/sunny0826/go-chatglm"
	"os"
)

func main() {
	apiKey := os.Getenv("API_KEY")
	m := chatglm.ModelAPI{
		Model:       chatglm.ChatGLMLite,
		TopP:        0.7,
		Temperature: 0.9,
		Prompt: []map[string]interface{}{
			{"role": "user", "content": "你好"},
			{"role": "assistant", "content": "我是人工智能助手"},
			{"role": "user", "content": "你叫什么名字"},
			{"role": "assistant", "content": "我叫chatGLM"},
			{"role": "user", "content": "你都可以做些什么事"},
		},
	}
	resp, err := m.Invoke(apiKey)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(resp)
}
Async
Start async invoke job
package main

import (
	"fmt"
	"os"

	"github.com/sunny0826/go-chatglm"
)

func main() {
	apiKey := os.Getenv("API_KEY")
	m := chatglm.ModelAPI{
		Model:       chatglm.ChatGLMLite,
		TopP:        0.7,
		Temperature: 1,
		Prompt: []map[string]interface{}{
			{"role": "user", "content": "你好"},
			{"role": "assistant", "content": "我是人工智能助手"},
			{"role": "user", "content": "你叫什么名字"},
			{"role": "assistant", "content": "我叫chatGLM"},
			{"role": "user", "content": "你都可以做些什么事"},
		},
	}
	resp, err := m.AsyncInvoke(apiKey)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(resp)
}

Response

map[code:200 data:map[request_id:7874899228181488751 task_id:75753966931688458417874899228181488752 task_status:PROCESSING] msg:操作成功 success:true]
Query async invoke result
package main

import (
	"fmt"
	"os"

	"github.com/sunny0826/go-chatglm"
)

func main() {
	apiKey := os.Getenv("API_KEY")
	taskID := "75753966931688458417874899228181488752"
	m := chatglm.ModelAPI{Model: chatglm.ChatGLMLite}
	resp, err := m.QueryAsyncInvokeResult(apiKey, taskID)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(resp)
}

Documentation

Index

Constants

View Source
const (
	BaseURL         = "https://open.bigmodel.cn/api/paas/v3/model-api"
	InvokeTypeSync  = "invoke"
	InvokeTypeAsync = "async-invoke"
	InvokeTypeSSE   = "sse-invoke"
	APITimeout      = 300 * time.Second
	ChatGLMLite     = "chatglm_lite"
	ChatGLMStd      = "chatglm_std"
	ChatGLMPro      = "chatglm_pro"
)
View Source
const (
	APITokenTTLSeconds = 3 * 60
	APIKeyPartCount    = 2
)

Variables

This section is empty.

Functions

func QueryAsyncInvokeResult added in v0.1.0

func QueryAsyncInvokeResult(apiKey, taskID string) (map[string]interface{}, error)

Types

type ModelAPI added in v0.1.0

type ModelAPI struct {
	Model       string
	Prompt      []map[string]interface{}
	TopP        float64
	Temperature float64
}

func (ModelAPI) AsyncInvoke added in v0.1.0

func (m ModelAPI) AsyncInvoke(apiKey string) (map[string]interface{}, error)

func (ModelAPI) Invoke added in v0.1.0

func (m ModelAPI) Invoke(apiKey string) (map[string]interface{}, error)

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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