LangChain.rb for Rails --- 为您的 Rails 应用程序添加 AI 的最快方法 ✨ 在几分钟内添加由 OpenAI 提供的问答功能。
LangChain.rb for Rails --- 为您的 Rails 应用程序添加 AI 的最快方法 ✨ 在几分钟内添加由 OpenAI 提供的问答功能。
The fastest way to sprinkle AI ✨ on top of your Rails app. Add OpenAI-powered question-and-answering in minutes.
Available for paid consulting engagements! Email me.
Install the gem and add to the application's Gemfile by executing:
bundle add langchainrb_railsIf bundler is not being used to manage dependencies, install the gem by executing:
gem install langchainrb_railsrails generate langchainrb_rails:pgvector --model=Product --llm=openaiThis adds required dependencies to your Gemfile, creates the config/initializers/langchainrb_rails.rb initializer file, database migrations, and adds the necessary code to the ActiveRecord model to enable vectorsearch.
bundle install && rails db:migrateOPENAI_API_KEY to your OpenAI API key: https://platform.openai.com/account/api-keysENV["OPENAI_API_KEY"]= Product.embed!This can take a while depending on the number of database records.
Product.ask("list the brands of shoes that are in stock")Returns a String with a natural language answer. The answer is assembled using the following steps:
question using the selected LLM.#as_vector representation )are added as context.Product.similarity_search("t-shirt")Returns ActiveRecord relation that most closely matches the query using vector search.
By default, embeddings are generated by calling the following method on your model instance:
to_json(except: :embedding)You can override this by defining an #as_vector method in your model:
def as_vector
{ name: name, description: description, category: category.name, ... }.to_json
endRe-generate embeddings after modifying this method:
Product.embed!rails generate langchainrb_rails:pgvector --model=Product --llm=openairails generate langchainrb_rails:pinecone --model=Product --llm=openairails generate langchainrb_rails:qdrant --model=Product --llm=openaiAvailable --llm options: cohere, google_palm, hugging_face, llama_cpp, ollama, openai, and replicate. The selected LLM will be used to generate embeddings and completions.
The --model option is used to specify which ActiveRecord model vectorsearch capabilities will be added to.
Pinecone Generator does the following:
config/initializers/langchainrb_rails.rb initializer filepinecone gem to the Gemfilerails generate langchainrb_rails:promptThis generator adds the following files to your Rails project:
Prompt model at app/models/prompt.rbprompts tableYou can then use the Prompt model to create and manage prompts for your model.
Example usage:
prompt = Prompt.create!(template: "Tell me a {adjective} joke about {subject}.")
prompt.render(adjective: "funny", subject: "elephants")
# => "Tell me a funny joke about elephants."This generator adds Langchain::Assistant-related ActiveRecord models, migrations, controllers, views and route to your Rails app. You can start creating assistants and chatting with them in immediately.
rails generate langchainrb_rails:assistant --llm=openaiAvailable --llm options: anthropic, cohere, google_palm, google_gemini, google_vertex_ai, hugging_face, llama_cpp, mistral_ai, ollama, openai, and replicate. The selected LLM will be used to generate completions.
To remove the generated files, run:
rails destroy langchainrb_rails:assistant暂无开放 Issues,或尚未同步最近议题。