Git Product home page Git Product logo

goai's Introduction

goai

Golang版本各厂商AI OpenAPI调用封装

普通调用

func TestChat(t *testing.T) {
	NewClient("apiKey", true)

	resp, err := Chat(&ChatRequest{
		Model: MODEL_VERSION_TURBO,
		Input: Input{
			Prompt: "你好",
		},
		Parameters: Parameters{
			ResultFormat: RESULT_FORMAT_MESSAGE,
		},
	})
	if err != nil {
		t.Errorf("err-> %v", err)
		return
	}
	t.Logf("ali: resp-> %#v", resp)
	t.Logf("ali: AI回复-> %s", resp.Output.Choices[0].Message.Content)
}

流式输出调用

func TestChatStream(t *testing.T) {
	NewClient("apiKey", true)

	streamReader, err := ChatStream(&ChatRequest{
		Model: MODEL_VERSION_TURBO,
		Input: Input{
			Prompt: "你好",
		},
		Parameters: Parameters{
			ResultFormat:      RESULT_FORMAT_MESSAGE,
			IncrementalOutput: true,
		},
	})
	if err != nil {
		t.Errorf("err-> %v", err)
		return
	}

	defer streamReader.Close()
	for {
		line, err := streamReader.Receive()
		if err != nil {
			t.Errorf("err-> %v", err)
			break
		}
		if streamReader.IsFinish() {
			t.Logf("read finish...")
			break
		}
		if streamReader.IsMaxEmptyLimit() {
			t.Errorf("read empty limit...")
			break
		}

		if len(line) == 0 {
			continue
		}
		t.Logf("ali: resp line-> %s, len-> %d", line, len(line))
	}
}

与HTTP Web框架结合使用

streamReader, err := aliyun.ChatStream(&aliyun.ChatRequest{
    Model: aliyun.MODEL_VERSION_TURBO,
    Input: aliyun.Input{
        Prompt: "你好",
    },
    Parameters: aliyun.Parameters{
        ResultFormat:      aliyun.RESULT_FORMAT_MESSAGE,
        IncrementalOutput: true,
    },
})
if err != nil {
    xlog.Errorf("chatAliStream: err-> %v", err)
    return nil, td.RerrInternalServer.SetReason(err.Error())
}

defer streamReader.Close()
ctx.Stream(func(w io.Writer) bool {
    line, err := streamReader.Receive()
    if err != nil {
        xlog.Errorf("chatAliStream: Receive err-> %v", err)
        return false
    }
    if streamReader.IsFinish() {
        xlog.Infof("chatAliStream: receive finish...")
        return false
    }
    if streamReader.IsMaxEmptyLimit() {
        xlog.Infof("chatAliStream: empty line limit...")
        return false
    }
    if len(line) == 0 {
        xlog.Infof("chatAliStream: line is empty...")
        return true
    }

    xlog.Infof("chatAliStream: line-> %s", string(line))

    // 写入一行数据到响应体
    w.Write(line)
    if flusher, ok := w.(http.Flusher); ok {
        // 确保数据发送到客户端
        flusher.Flush()
    }

    // 继续处理下一行
    return true
})

goai's People

Contributors

swxctx avatar

Stargazers

eagle avatar

Watchers

 avatar

Forkers

txxzx kyliu2023

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.