Giter Club home page Giter Club logo

chatgpt-streaming's Introduction

chatGPT_streaming

golang使用openai的ChatGPT接口实践,使用流式传输,类似ChatGPT网页效果,并且可以让ChatGPT的服务单独部署(单独部署到非大中华区的服务器),并用grpc streaming 做中间层,保证不受墙的影响。使用websocket最终通信,后端考虑多种情况,健壮可用。 非常方便移植到你自己的项目,赶紧来看看吧!

演示

日志 GIF演示

前置知识

  • http text/event-stream
  • Streaming with gRPC
  • websocket
  • go routine and channel

event_stream

访问后端使用的是text/event-stream

配置:

  • BaseURL默认是:https://api.openai.com/v1
  • token: 使用自己注册的openai的token,注意不要泄漏自己的token
  • 可以选择代理请求,方便本地测试,详细逻辑见具体代码
      proxyUrl, err := url.Parse("http://127.0.0.1:10887")
      if err != nil {
          log.Fatal().Msgf("failed to init proxy: %v", err)
          return
      }
      http.DefaultTransport = &http.Transport{Proxy: http.ProxyURL(proxyUrl)}
      config.HTTPClient.Transport = http.DefaultTransport
      client := goopenai.NewClientWithConfig(config)

启动:

  • 直接执行main()函数
  • 然后打开ws_demo.html
  • 愉快的对话了,注意我使用了context上下文传递,超过4090token数量会接口抛错,具体逻辑请自行修改

grpc_stream

配置:

  • 在grpc_stream/grpc.go 或者 grpc_stream/grpc/grpc_server.go 配置常量Token
  • 代理设置grpc_stream/grpc/grpc_server.go StreamChatCompletion方法里

启动:

  • 先启动grpc服务:grpc_stream/grpc/grpc_server.go main()
  • 再启动webscket服务端:grpc_stream/grpc.go main()
  • 然后打开ws_demo.html
  • 愉快的对话了,注意我使用了context上下文传递,超过4090token数量会接口抛错,具体逻辑请自行修改

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.