gpt4 book ai didi

ruby-on-rails - Ruby on Rails 中的 ruby​​-openai api gem : how to implement a streaming conversation?

转载 作者:行者123 更新时间:2023-12-02 22:48:00 25 4
gpt4 key购买 nike

Openai 提供了一个 API,允许您实现 ChaGPT 或 DAL-E 等 AI 服务。对于 Ruby on Rails 应用程序,有几个可用的 gem,其中有 ruby-openai

它工作得很好,但唯一的问题是它没有流对话功能,这意味着您一次只能发送一个问题请求,而没有任何对话历史记录。换句话说,API 会忘记您在发送回复后提出的每个问题。

那么我们该如何解决这个问题呢?

最佳答案

基本上,您需要自己实现整个行为。以下是所有实现步骤,包括 dal-e ai 的实现,其响应包含多张图片而不是一张。

您还可以找到我的整个存储库 HERE 并克隆应用程序!!!

实现流对话功能

基本实现

查看Doug伯克利的Notion Page API的基本实现

实现流式对话

默认情况下,openai gem 不具备该功能,因此必须自己实现

  1. 使用 3 个表(对话、问题、答案)创建数据库,其结构如下:
# schema.rb
ActiveRecord::Schema[7.0].define(version: 2023_05_29_194913) do
create_table "answers", force: :cascade do |t|
t.text "content"
t.integer "question_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["question_id"], name: "index_answers_on_question_id"
end

create_table "conversations", force: :cascade do |t|
t.text "initial_question"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.text "historic"
end

create_table "questions", force: :cascade do |t|
t.text "content"
t.integer "conversation_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["conversation_id"], name: "index_questions_on_conversation_id"
end

add_foreign_key "answers", "questions"
add_foreign_key "questions", "conversations"
end
  • 路线
  • Rails.application.routes.draw do
    root "pages#home" # supposes that you have a pages controller with a home action
    resources :conversations, only: [:create, :show]
    post "question", to: "conversations#ask_question"
    end
  • 主页 View (只有一个重定向到创建对话操作的按钮 - 见下文)
  • <h1>Let's talk</h1>
    <%= button_to "Create New Conversation", conversations_path, method: :post, class: "btn btn-primary my-3" %>
  • Controller app/controllers/conversations_controller.rb
  • class ConversationsController < ApplicationController
    def create
    @convo = Conversation.create
    redirect_to conversation_path(@convo)
    end

    def show
    @convo = Conversation.find(params[:id])
    end

    def ask_question
    @question = Question.new(content: params[:entry])
    conversation = Conversation.find(params[:conversation])
    @question.conversation = conversation
    @question.save
    if conversation.historic.nil?
    response = OpenaiService.new(params[:entry]).call
    conversation.historic = "#{@question.content}\n#{response}"
    else
    response = OpenaiService.new("#{conversation.historic}\n#{params[:entry]}").call
    conversation.historic += "\n#{@question.content}\n#{response}"
    end
    conversation.save
    @answer = Answer.create(content: response, question: @question)
    redirect_to conversation_path(conversation)
    end
    end
  • 显示页面app/views/conversations/show.html.erb
  • <h1>This is your conversation</h1>
    <p>Ask your question</p>
    <form action="<%= question_path %>", method="post">
    <input type="hidden" name="conversation" value="<%= @convo.id %>">
    <textarea rows="5" cols="33" name="entry"></textarea>
    <input type="submit" class="btn btn-primary">
    </form>

    <br>

    <ul>
    <% @convo.questions.each do |question| %>
    <li>
    Q: <%= question.content.capitalize %> <%= "?" if question.content.strip.last != "?" %>
    </li>
    <li>
    A: <%= question.answers.first.content %>
    </li>
    <% end %>
    </ul>

    <%= link_to "Back", root_path %>

  • rails 并测试:)
  • 资源:

    更进一步:

    关于ruby-on-rails - Ruby on Rails 中的 ruby​​-openai api gem : how to implement a streaming conversation?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76383308/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com