ai

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2024 License: MIT Imports: 23 Imported by: 0

README

低代码AI能力工具包

命令行工具

Install
go install apigo.cc/ai/ai/ai@latest
Usage
ai -h | --help		show usage
ai -e | --export	export ai.ts file to ./lib for develop
ai test | test.js	run test.js, if not specified, run ./ai.js
Sample
test.js
import {glm} from './lib/ai'
import console from './lib/console'

function main(...args) {
    if(!args[0]) throw new Error('no ask')
    let r = glm.fastAsk(args[0], r => {
        console.print(r)
    })
    console.println()
    return r
}
run sample
ai test "你好"
Module
mod/glm.js
import {glm} from './lib/ai'
import console from './lib/console'

function chat(...args) {
    if(!args[0]) throw new Error('no prompt')
    return glm.fastAsk(args[0])
}

function draw(...args) {
    if(!args[0]) throw new Error('no ask')
    return glm.makeImage(args[0], {size:'1024x1024'})
}

module.exports = {chat, draw}
test.js
import glm from './mod/glm'

function main(...args) {
    console.println(glm.chat(args[0]))
}
run sample
ai test "你好"
Configure
llm.yml
openai:
  apiKey: ...
zhipu:
  apiKey: ...
or use env.yml
llm.yml
llm:
  openai:
    apiKey: ...
  zhipu:
    apiKey: ...
encrypt apiKey

install sskey

go install github.com/ssgo/tool/sskey@latest
sskey -e 'your apiKey'

copy url base64 format encrypted apiKey into llm.yml or env.yml

config with special endpoint
llm:
  openai:
    apiKey: ...
    endpoint: https://api.openai.com/v1
config multi api
llm:
  glm:
    apiKey: ...
    llm: zhipu
  glm2:
    apiKey: ...
    endpoint: https://......
    llm: zhipu

调用 JavaScript API

Install
go get apigo.cc/ai/ai
Usage
package main

import (
	"apigo.cc/ai/ai/js"
	"fmt"
)

func main() {
    result, err := js.Run(`return ai.glm.fastAsk(args[0])`, "", "你好")
    // js.RunFile
    // js.StartFromFile
    // js.StartFromCode
    if err != nil {
        fmt.Println(err.Error())
    } else if result != nil {
        fmt.Println(result)
    }
}

调用 Go API

Install
go get apigo.cc/ai/ai
Usage
package main

import (
	"apigo.cc/ai/ai"
	"apigo.cc/ai/ai/llm"
	"fmt"
)

func main() {
	ai.Init()
	glm := llm.Get("zhipu")

	r, usage, err := glm.FastAsk(llm.Messages().User().Text("你是什么模型").Make(), func(text string) {
		fmt.Print(text)
	})

	if err != nil {
		fmt.Println(err)
	} else {
		fmt.Println()
		fmt.Println("result:", r)
		fmt.Println("usage:", usage)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExportForDev added in v0.0.3

func ExportForDev() (string, error)

func Init added in v0.0.2

func Init()

func InitFrom added in v0.0.2

func InitFrom(filePath string)

func Run added in v0.0.3

func Run(code string, refFile string, args ...any) (any, error)

func RunFile added in v0.0.3

func RunFile(file string, args ...any) (any, error)

func SetSSKey

func SetSSKey(key, iv []byte)

Types

type Exports added in v0.0.3

type Exports struct {
	LLMList []string
}

type Runtime added in v0.0.3

type Runtime struct {
	// contains filtered or unexported fields
}

func New added in v0.0.3

func New() *Runtime

func (*Runtime) GetCallStack added in v0.0.3

func (rt *Runtime) GetCallStack() []string

func (*Runtime) RunCode added in v0.0.3

func (rt *Runtime) RunCode(code string) (any, error)

func (*Runtime) RunMain added in v0.0.3

func (rt *Runtime) RunMain(args ...any) (any, error)

func (*Runtime) SetModuleLoader added in v0.0.3

func (rt *Runtime) SetModuleLoader(fn func(filename string) string)

func (*Runtime) StartFromCode added in v0.0.3

func (rt *Runtime) StartFromCode(code, refFile string) (any, error)

func (*Runtime) StartFromFile added in v0.0.3

func (rt *Runtime) StartFromFile(file string) (any, error)

type WatchRunner added in v0.0.5

type WatchRunner struct {
	// contains filtered or unexported fields
}

func WatchRun added in v0.0.5

func WatchRun(file string, extDirs, extTypes []string, args ...any) (*WatchRunner, error)

func (*WatchRunner) Stop added in v0.0.5

func (wr *WatchRunner) Stop()

func (*WatchRunner) WaitForKill added in v0.0.5

func (wr *WatchRunner) WaitForKill()

Directories

Path Synopsis
ast
Package ast declares types representing a JavaScript AST.
Package ast declares types representing a JavaScript AST.
file
Package file encapsulates the file abstractions used by the ast & parser.
Package file encapsulates the file abstractions used by the ast & parser.
ftoa
Package ftoa provides ECMAScript-compliant floating point number conversion to string.
Package ftoa provides ECMAScript-compliant floating point number conversion to string.
ftoa/internal/fast
Package fast contains code ported from V8 (https://github.com/v8/v8/blob/master/src/numbers/fast-dtoa.cc)
Package fast contains code ported from V8 (https://github.com/v8/v8/blob/master/src/numbers/fast-dtoa.cc)
parser
Package parser implements a parser for JavaScript.
Package parser implements a parser for JavaScript.
token
Package token defines constants representing the lexical tokens of JavaScript (ECMA5).
Package token defines constants representing the lexical tokens of JavaScript (ECMA5).
unistring
Package unistring contains an implementation of a hybrid ASCII/UTF-16 string.
Package unistring contains an implementation of a hybrid ASCII/UTF-16 string.
goja_nodejs
url
interface
llm
llm

Jump to

Keyboard shortcuts

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