Ruby on Rails中的ruby-openai API gem:如何实现流会话?

tgabmvqs  于 2023-06-05  发布在  Ruby
关注(0)|答案(1)|浏览(253)

Openai提供了一个API,允许您实现AI服务,如ChaGPT或DAL-E。对于Ruby on Rails应用程序,有几个可用的gem,其中一个是ruby-openai
它工作得很好,但唯一的问题是它没有流会话功能,这意味着你一次只能发送一个问题请求,而没有任何会话的历史跟踪。换句话说,API在发送回复后会忘记您提出的每个问题。
那我们该怎么解决呢

ogsagwnx

ogsagwnx1#

基本上你需要自己实现整个行为。这里是所有的实现步骤,包括dal-e ai的实现,其中响应有几张图片,而不是只有一张。

您也可以找到我的整个仓库HERE,克隆应用!!!

实现流会话功能

基本实现

查看Doug Berkley的Notion Page,了解API的基本实现

实现流会话

默认情况下,openai gem不附带该特性,因此必须自己实现它。
1.创建您的数据库与3个表(对话,问题,答案)与thw以下结构:

# 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

1.路线

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

1.主页视图(只有一个重定向到CreateConversation操作的按钮--见下文)

<h1>Let's talk</h1>
<%= button_to "Create New Conversation", conversations_path, method: :post, class: "btn btn-primary my-3" %>

1.控制器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

1.显示页面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 %>
  1. rails s和测试:)

相关资源:

更进一步:

相关问题