### LLM Vendor Setup and Instantiation Source: https://github.com/patterns-ai-core/langchainrb/wiki/2.-LLMs This section details how to set up and instantiate different LLM classes provided by Langchain.rb. It includes the necessary gem installations, environment variable requirements, and example code for initializing various LLM clients. ```APIDOC ## Langchain.rb LLM Integration Langchain.rb provides a common interface to interact with all supported Large Language Models (LLMs). ### Supported LLM Vendors | Name | ENV Requirements | Gem Requirements | | :------------ | :-------------- | :-------------- | | [AI21](https://www.ai21.com/) | `ENV["AI21_API_KEY"]` | `gem "ai21", "~> 0.2.0"` | | [Cohere](https://cohere.ai/) | `ENV["COHERE_API_KEY"]` | `gem "cohere-ruby", "~> 0.9.3"` | | [Google PaLM](https://ai.google/discover/palm2) | `ENV["MILVUS_URL"]` | `gem "google_palm_api", "~> 0.1.0"` | | [Hugging Face](https://huggingface.co/) | `ENV["HUGGING_FACE_API_KEY"]` | `gem "hugging-face", "~> 0.3.2"` | | [OpenAI](https://openai.com/) | `ENV["OPENAI_API_KEY"]` | `gem "ruby-openai", "~> 4.0.0"` | | [Replicate](https://replicate.com/) | `ENV["REPLICATE_API_KEY"]` | `gem "replicate-ruby", "~> 0.2.2"` | ### Usage 1. Pick an LLM from the list above and install the gem listed under **Gem Requirements** 2. Set the environment variable listed under **ENV Requirements** 3. Instantiate the LLM class: ```ruby openai = Langchain::LLM::OpenAI.new(api_key: ENV["OPENAI_API_KEY"]) cohere = Langchain::LLM::Cohere.new(api_key: ENV["COHERE_API_KEY"]) hugging_face = Langchain::LLM::HuggingFace.new(api_key: ENV["HUGGING_FACE_API_KEY"]) replicate = Langchain::LLM::Replicate.new(api_key: ENV["REPLICATE_API_KEY"]) google_palm = Langchain::LLM::GooglePalm.new(api_key: ENV["GOOGLE_PALM_API_KEY"]) ai21 = Langchain::LLM::AI21.new(api_key: ENV["AI21_API_KEY"]) ``` ``` -------------------------------- ### Manage Few Shot Prompt Templates Source: https://github.com/patterns-ai-core/langchainrb/blob/main/README.md Create templates that include example pairs to guide model output. ```ruby prompt = Langchain::Prompt::FewShotPromptTemplate.new( prefix: "Write antonyms for the following words.", suffix: "Input: {adjective}\nOutput:", example_prompt: Langchain::Prompt::PromptTemplate.new( input_variables: ["input", "output"], template: "Input: {input}\nOutput: {output}" ), examples: [ { "input": "happy", "output": "sad" }, { "input": "tall", "output": "short" } ], input_variables: ["adjective"] ) prompt.format(adjective: "good") # Write antonyms for the following words. # # Input: happy # Output: sad # # Input: tall # Output: short # # Input: good # Output: ``` ```ruby prompt.save(file_path: "spec/fixtures/prompt/few_shot_prompt_template.json") ``` ```ruby prompt = Langchain::Prompt.load_from_path(file_path: "spec/fixtures/prompt/few_shot_prompt_template.json") prompt.prefix # "Write antonyms for the following words." ``` -------------------------------- ### Create Few-Shot Prompt Template Source: https://github.com/patterns-ai-core/langchainrb/wiki/1.-Prompts Initialize a FewShotPromptTemplate with a prefix, suffix, example prompt structure, and a list of examples. The format method generates the full prompt including examples. ```ruby prompt = Langchain::Prompt::FewShotPromptTemplate.new( prefix: "Write antonyms for the following words.", suffix: "Input: {adjective}\nOutput:", example_prompt: Langchain::Prompt::PromptTemplate.new( input_variables: ["input", "output"], template: "Input: {input}\nOutput: {output}" ), examples: [ { "input": "happy", "output": "sad" }, { "input": "tall", "output": "short" } ], input_variables: ["adjective"] ) prompt.format(adjective: "good") # Write antonyms for the following words. # # Input: happy # Output: sad # # Input: tall # Output: short # # Input: good # Output: ``` -------------------------------- ### Initialize LLM Client Source: https://github.com/patterns-ai-core/langchainrb/blob/main/README.md Example of initializing an OpenAI client with an API key and default configuration. ```ruby llm = Langchain::LLM::OpenAI.new( api_key: ENV["OPENAI_API_KEY"], default_options: { temperature: 0.7, chat_model: "gpt-4o" } ) ``` -------------------------------- ### Install Langchain.rb Source: https://github.com/patterns-ai-core/langchainrb/blob/main/README.md Commands to add the gem to your project or install it globally. ```shell bundle add langchainrb ``` ```shell gem install langchainrb ``` -------------------------------- ### Install Tool Dependencies Source: https://github.com/patterns-ai-core/langchainrb/wiki/7.-Experimental Install the necessary gems for the calculator, search, and wikipedia tools. ```bash # To use all 3 tools: gem install eqn gem install google_search_results gem install wikipedia-client ``` -------------------------------- ### Install Langchain.rb Source: https://github.com/patterns-ai-core/langchainrb/wiki/Home Methods to install the gem via Bundler or directly via RubyGems. ```ruby $ bundle add langchainrb ``` ```ruby $ gem install langchainrb ``` -------------------------------- ### Instantiate Other Vector Search Clients Source: https://github.com/patterns-ai-core/langchainrb/blob/main/README.md Examples of instantiating clients for other supported vector search databases. Ensure the corresponding gem is added as a dependency. ```ruby client = Langchain::Vectorsearch::Chroma.new(...) # `gem "chroma-db", "~> 0.6.0"` ``` ```ruby client = Langchain::Vectorsearch::Hnswlib.new(...) # `gem "hnswlib", "~> 0.8.1"` ``` ```ruby client = Langchain::Vectorsearch::Milvus.new(...) # `gem "milvus", "~> 0.9.3"` ``` ```ruby client = Langchain::Vectorsearch::Pinecone.new(...) # `gem "pinecone", "~> 1.0"` ``` ```ruby client = Langchain::Vectorsearch::Pgvector.new(...) # `gem "pgvector", "~> 0.2"` ``` ```ruby client = Langchain::Vectorsearch::Qdrant.new(...) # `gem "qdrant-ruby", "~> 0.9.3"` ``` ```ruby client = Langchain::Vectorsearch::Elasticsearch.new(...) # `gem "elasticsearch", "~> 8.2.0"` ``` -------------------------------- ### Instantiate LLM for Embeddings Source: https://github.com/patterns-ai-core/langchainrb/blob/main/README.md Choose and instantiate your preferred LLM provider for generating embeddings. This example uses OpenAI. ```ruby llm = Langchain::LLM::OpenAI.new(api_key: ENV["OPENAI_API_KEY"]) ``` -------------------------------- ### Install Unicode Gem with CFLAGS Source: https://github.com/patterns-ai-core/langchainrb/blob/main/README.md Troubleshoot installation issues with the 'unicode' gem, which is required by 'pragmatic_segmenter', by providing specific CFLAGS to the gem installation command. ```bash gem install unicode -- --with-cflags="-Wno-incompatible-function-pointer-types" ``` -------------------------------- ### Example Usage of Custom Movie Tool with Langchain::Assistant Source: https://github.com/patterns-ai-core/langchainrb/blob/main/README.md Instantiate a custom tool and integrate it with Langchain::Assistant to enable the assistant to perform movie-related queries. The assistant's response can be found in the last message. ```ruby movie_tool = MovieInfoTool.new(api_key: "...") assistant = Langchain::Assistant.new( llm: llm, instructions: "You're a helpful AI assistant that can provide movie information", tools: [movie_tool] ) assistant.add_message_and_run(content: "Can you tell me about the movie 'Inception'?") # Check the response in the last message in the conversation assistant.messages.last ``` -------------------------------- ### Vector Database Integration Source: https://github.com/patterns-ai-core/langchainrb/wiki/5.-Vector-Databases This section details how to set up and use different vector databases with Langchain.rb. It includes a list of supported databases, their environment and gem requirements, and examples of how to instantiate them. ```APIDOC ## Supported Vector Databases Langchain.rb provides a common interface to interact with all supported vector databases. | Name | ENV Requirements | Gem Requirements | | :------------ | :-------------- | :-------------- | | [Chroma](https://trychroma.com/) | `ENV["CHROMA_URL"]` | `gem "chroma-db", "~> 0.3.0"` | | [Milvus](https://milvus.io/) | `ENV["MILVUS_URL"]` | `gem "milvus", "~> 0.9.0"` | | [Pinecone](https://www.pinecone.io/) | `ENV["PINECONE_API_KEY"]`, `ENV["PINECONE_ENVIRONMENT"]` | `gem "pinecone", "~> 0.1.6"` | | [Qdrant](https://qdrant.tech/) | `ENV["QDRANT_URL"]`, `ENV["QDRANT_API_KEY"]` | `gem "qdrant-ruby", "~> 0.9.0"` | [Weaviate](https://weaviate.io/) | `ENV["WEAVIATE_URL"]`, `ENV["WEAVIATE_API_KEY"]` | `gem "weaviate-ruby", "~> 0.8.0"` | | [Pgvector](https://github.com/pgvector/pgvector) | `ENV["POSTGRES_URL"]` | `gem "pgvector", "~> 0.2"` | ## Usage 1. Pick a vector database from list above and install the gem listed under **Gem Requirements** 2. Set the environment variable(s) listed under **ENV Requirements** 3. Instantiate the vector database class: ```ruby weaviate = Langchain::Vectorsearch::Weaviate.new( url: ENV["WEAVIATE_URL"], api_key: ENV["WEAVIATE_API_KEY"], index_name: "Documents", llm: :openai, # or :cohere, :hugging_face, :google_palm, or :replicate llm_api_key: ENV["OPENAI_API_KEY"] # API key for the selected LLM ) # You can instantiate other supported vector databases the same way: milvus = Langchain::Vectorsearch::Milvus.new(...) qdrant = Langchain::Vectorsearch::Qdrant.new(...) pinecone = Langchain::Vectorsearch::Pinecone.new(...) chrome = Langchain::Vectorsearch::Chroma.new(...) pgvector = Langchain::Vectorsearch::Pgvector.new(...) ``` ``` -------------------------------- ### Instantiate Weaviate Vector Database Source: https://github.com/patterns-ai-core/langchainrb/wiki/5.-Vector-Databases Instantiate the Weaviate vector database client. Ensure the necessary environment variables are set and the `weaviate-ruby` gem is installed. You can select different LLMs and provide their API keys. ```ruby weaviate = Langchain::Vectorsearch::Weaviate.new( url: ENV["WEAVIATE_URL"], api_key: ENV["WEAVIATE_API_KEY"], index_name: "Documents", llm: :openai, # or :cohere, :hugging_face, :google_palm, or :replicate llm_api_key: ENV["OPENAI_API_KEY"] # API key for the selected LLM ) ``` -------------------------------- ### Create Default Schema Source: https://github.com/patterns-ai-core/langchainrb/blob/main/README.md Creates the default schema for the vector search database. This is a common setup step before adding data. ```ruby client.create_default_schema ``` -------------------------------- ### Ask a Question with Context Source: https://github.com/patterns-ai-core/langchainrb/wiki/5.-Vector-Databases Use the `ask` method to get an answer to a question. It generates an embedding for the question, finds relevant context from the vector database, and passes both to the LLM for a generated response. ```ruby client.ask( question: ) ``` -------------------------------- ### Define Custom MovieInfoTool in Ruby Source: https://github.com/patterns-ai-core/langchainrb/blob/main/README.md Create a custom tool by extending Langchain::ToolDefinition and implementing required methods for specific functionalities like searching movies or getting movie details. ```ruby class MovieInfoTool extend Langchain::ToolDefinition define_function :search_movie, description: "MovieInfoTool: Search for a movie by title" do property :query, type: "string", description: "The movie title to search for", required: true end define_function :get_movie_details, description: "MovieInfoTool: Get detailed information about a specific movie" do property :movie_id, type: "integer", description: "The TMDb ID of the movie", required: true end def initialize(api_key:) @api_key = api_key end def search_movie(query:) tool_response(...) end def get_movie_details(movie_id:) tool_response(...) end end ``` -------------------------------- ### Parse LLM Response with Structured Output Parser Source: https://github.com/patterns-ai-core/langchainrb/blob/main/README.md After getting a response from the LLM, use the parser's parse method to convert the LLM's output into a structured Ruby object. Ensure the LLM response adheres to the defined JSON schema. ```ruby llm = Langchain::LLM::OpenAI.new(api_key: ENV["OPENAI_API_KEY"]) llm_response = llm.chat(messages: [{role: "user", content: prompt_text}]).completion parser.parse(llm_response) ``` -------------------------------- ### Initialize and Run Assistant Source: https://github.com/patterns-ai-core/langchainrb/blob/main/README.md Demonstrates initializing an assistant with an LLM and tools, adding messages, and executing runs with optional streaming. ```ruby llm = Langchain::LLM::OpenAI.new(api_key: ENV["OPENAI_API_KEY"]) assistant = Langchain::Assistant.new( llm: llm, instructions: "You're a helpful AI assistant", tools: [Langchain::Tool::NewsRetriever.new(api_key: ENV["NEWS_API_KEY"])] ) # Add a user message and run the assistant assistant.add_message_and_run!(content: "What's the latest news about AI?") # Supply an image to the assistant assistant.add_message_and_run!( content: "Describe this image.", image_url: "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg" ) # Access the conversation thread messages = assistant.messages # Run the assistant with automatic tool execution assistant.run(auto_tool_execution: true) # If you want to stream the response, you can add a response handler assistant = Langchain::Assistant.new( llm: llm, instructions: "You're a helpful AI assistant", tools: [Langchain::Tool::NewsRetriever.new(api_key: ENV["NEWS_API_KEY"])] ) do |response_chunk| # ...handle the response stream # print(response_chunk.inspect) end assistant.add_message(content: "Hello") assistant.run(auto_tool_execution: true) ``` -------------------------------- ### Instantiate Other Supported Vector Databases Source: https://github.com/patterns-ai-core/langchainrb/wiki/5.-Vector-Databases Demonstrates how to instantiate other supported vector databases like Milvus, Qdrant, Pinecone, and Chroma using a similar pattern to Weaviate. ```ruby # You can instantiate other supported vector databases the same way: milvus = Langchain::Vectorsearch::Milvus.new(...) qdrant = Langchain::Vectorsearch::Qdrant.new(...) pinecone = Langchain::Vectorsearch::Pinecone.new(...) chrome = Langchain::Vectorsearch::Chroma.new(...) pgvector = Langchain::Vectorsearch::Pgvector.new(...) ``` -------------------------------- ### Instantiate Agent with Tools Source: https://github.com/patterns-ai-core/langchainrb/wiki/7.-Experimental Pass the desired tools during the agent instantiation process. ```ruby agent = Langchain::Agent::ChainOfThoughtAgent.new( llm: :openai, # or :cohere, :hugging_face, :google_palm or :replicate llm_api_key: ENV["OPENAI_API_KEY"], tools: ["search", "calculator", "wikipedia"] ) ``` -------------------------------- ### Create Prompt with Multiple Input Variables Source: https://github.com/patterns-ai-core/langchainrb/wiki/1.-Prompts Initialize a PromptTemplate with multiple input variables by passing an array of strings to the input_variables argument. Use the format method to supply values for each variable. ```ruby prompt = Langchain::Prompt::PromptTemplate.new(template: "Tell me a {adjective} joke about {content}. ", input_variables: ["adjective", "content"]) prompt.format(adjective: "funny", content: "chickens") # "Tell me a funny joke about chickens." ``` -------------------------------- ### Instantiate Weaviate Client Source: https://github.com/patterns-ai-core/langchainrb/blob/main/README.md Instantiate the Weaviate client with your Weaviate instance URL, API key, desired index name, and the LLM provider. ```ruby client = Langchain::Vectorsearch::Weaviate.new( url: ENV["WEAVIATE_URL"], api_key: ENV["WEAVIATE_API_KEY"], index_name: "Documents", llm: llm ) ``` -------------------------------- ### Initialize LLM Providers in Ruby Source: https://github.com/patterns-ai-core/langchainrb/blob/main/README.md Instantiate different LLM provider classes using their respective API keys. ```ruby # Using Anthropic anthropic_llm = Langchain::LLM::Anthropic.new(api_key: ENV["ANTHROPIC_API_KEY"]) # Using Google Gemini gemini_llm = Langchain::LLM::GoogleGemini.new(api_key: ENV["GOOGLE_GEMINI_API_KEY"]) # Using OpenAI openai_llm = Langchain::LLM::OpenAI.new(api_key: ENV["OPENAI_API_KEY"]) ``` -------------------------------- ### Initialize and Score RAG Pipeline with Ragas Source: https://github.com/patterns-ai-core/langchainrb/blob/main/README.md Set up the Ragas evaluator using a specified LLM and then score the performance of a RAG pipeline by providing the generated answer, the original question, and the retrieved context. ```ruby # We recommend using Langchain::LLM::OpenAI as your llm for Ragas ragas = Langchain::Evals::Ragas::Main.new(llm: llm) # The answer that the LLM generated # The question (or the original prompt) that was asked # The context that was retrieved (usually from a vectorsearch database) ragas.score(answer: "", question: "", context: "") # => # { # ragas_score: 0.6601257446503674, # answer_relevance_score: 0.9573145866787608, # context_relevance_score: 0.6666666666666666, # faithfulness_score: 0.5 # } ``` -------------------------------- ### Create and Format Prompt Templates Source: https://github.com/patterns-ai-core/langchainrb/blob/main/README.md Define templates with input variables or static text and format them for use. ```ruby prompt = Langchain::Prompt::PromptTemplate.new(template: "Tell me a {adjective} joke about {content}.", input_variables: ["adjective", "content"]) prompt.format(adjective: "funny", content: "chickens") # "Tell me a funny joke about chickens." ``` ```ruby prompt = Langchain::Prompt::PromptTemplate.from_template("Tell me a funny joke about chickens.") prompt.input_variables # [] prompt.format # "Tell me a funny joke about chickens." ``` -------------------------------- ### Configure Assistant Callbacks Source: https://github.com/patterns-ai-core/langchainrb/blob/main/README.md Sets up hooks for monitoring message additions and tool execution events. ```ruby assistant.add_message_callback = -> (message) { puts "New message: #{message}" } ``` ```ruby assistant.tool_execution_callback = -> (tool_call_id, tool_name, method_name, tool_arguments) { puts "Executing tool_call_id: #{tool_call_id}, tool_name: #{tool_name}, method_name: #{method_name}, tool_arguments: #{tool_arguments}" } ``` -------------------------------- ### Instantiate LLM Clients Source: https://github.com/patterns-ai-core/langchainrb/wiki/2.-LLMs Initialize specific LLM provider classes using their respective API keys. ```ruby openai = Langchain::LLM::OpenAI.new(api_key: ENV["OPENAI_API_KEY"]) cohere = Langchain::LLM::Cohere.new(api_key: ENV["COHERE_API_KEY"]) hugging_face = Langchain::LLM::HuggingFace.new(api_key: ENV["HUGGING_FACE_API_KEY"]) replicate = Langchain::LLM::Replicate.new(api_key: ENV["REPLICATE_API_KEY"]) google_palm = Langchain::LLM::GooglePalm.new(api_key: ENV["GOOGLE_PALM_API_KEY"]) ai21 = Langchain::LLM::AI21.new(api_key: ENV["AI21_API_KEY"]) ``` -------------------------------- ### Configure Environment Variables Source: https://github.com/patterns-ai-core/langchainrb/wiki/7.-Experimental Set the required API keys for external tool services. ```bash export SERPAPI_API_KEY=paste-your-serpapi-api-key-here ``` -------------------------------- ### Add Data from Multiple File Paths Source: https://github.com/patterns-ai-core/langchainrb/wiki/5.-Vector-Databases Add data to the vector database from various file types (PDF, TXT, DOCX, CSV) by providing a list of file paths to the `add_data` method. Ensure the `Langchain.root` path is correctly configured. ```ruby my_pdf = Langchain.root.join("path/to/my.pdf") my_text = Langchain.root.join("path/to/my.txt") my_docx = Langchain.root.join("path/to/my.docx") my_csv = Langchain.root.join("path/to/my.csv") weaviate.add_data(paths: [my_pdf, my_text, my_docx, my_csv]) ``` -------------------------------- ### Create Prompt with One Input Variable Source: https://github.com/patterns-ai-core/langchainrb/wiki/1.-Prompts Use Langchain::Prompt::PromptTemplate.new to create a prompt with a single input variable. The format method is used to insert values into the template. ```ruby prompt = Langchain::Prompt::PromptTemplate.new(template: "Tell me a {adjective} joke.", input_variables: ["adjective"]) prompt.format(adjective: "funny") # "Tell me a funny joke." ``` -------------------------------- ### Initialize Chain of Thought Agent Source: https://github.com/patterns-ai-core/langchainrb/wiki/7.-Experimental Instantiate a new agent with a specified LLM and a list of available tools. ```ruby agent = Langchain::Agent::ChainOfThoughtAgent.new( llm: :openai, # or :cohere, :hugging_face, :google_palm or :replicate llm_api_key: ENV["OPENAI_API_KEY"], tools: ["search", "calculator", "wikipedia"] ) agent.tools # => ["search", "calculator", "wikipedia"] ``` -------------------------------- ### Verify Agent Tools Source: https://github.com/patterns-ai-core/langchainrb/wiki/7.-Experimental Check the list of tools currently registered to the agent instance. ```ruby agent.tools # => ["search", "calculator", "wikipedia"] ``` -------------------------------- ### POST /complete Source: https://github.com/patterns-ai-core/langchainrb/blob/main/README.md Generates text completions based on a provided prompt. ```APIDOC ## POST /complete ### Description Generates a completion for a given prompt string. ### Method POST ### Parameters #### Request Body - **prompt** (string) - Required - The input prompt for completion. - **max_tokens** (integer) - Optional - The maximum number of tokens to generate. - **temperature** (float) - Optional - Controls randomness (0.0 to 1.0). - **top_p** (float) - Optional - Controls diversity of generated tokens. - **n** (integer) - Optional - Number of completions to generate. - **stop** (array) - Optional - Sequences where the API will stop generating. - **presence_penalty** (float) - Optional - Penalizes new tokens based on presence. - **frequency_penalty** (float) - Optional - Penalizes new tokens based on frequency. ### Response #### Success Response (200) - **completion** (string) - The generated text completion. ``` -------------------------------- ### Load Prompt Template from JSON Source: https://github.com/patterns-ai-core/langchainrb/wiki/1.-Prompts Load a PromptTemplate from a JSON file using Langchain::Prompt.load_from_path. The input_variables are accessible after loading. ```ruby prompt = Langchain::Prompt.load_from_path(file_path: "spec/fixtures/prompt/prompt_template.json") prompt.input_variables # ["adjective", "content"] ``` -------------------------------- ### Load Prompt Template from YAML Source: https://github.com/patterns-ai-core/langchainrb/wiki/1.-Prompts Load a prompt template from a YAML file using Langchain::Prompt.load_from_path. The input_variables are accessible after loading. ```ruby prompt = Langchain::Prompt.load_from_path(file_path: "spec/fixtures/prompt/prompt_template.yaml") prompt.input_variables #=> ["adjective", "content"] ``` -------------------------------- ### Create PromptTemplate from Template String Source: https://github.com/patterns-ai-core/langchainrb/wiki/1.-Prompts Use Langchain::Prompt::PromptTemplate.from_template to create a prompt template directly from a template string. The input variables are automatically inferred. ```ruby prompt = Langchain::Prompt::PromptTemplate.from_template("Tell me a {adjective} joke about {content}.") prompt.input_variables # ["adjective", "content"] prompt.format(adjective: "funny", content: "chickens") # "Tell me a funny joke about chickens." ``` -------------------------------- ### Configure Logging Source: https://github.com/patterns-ai-core/langchainrb/wiki/Home Adjust the logging level for the library. ```ruby Langchain.logger.level = :info ``` -------------------------------- ### Load Few-Shot Prompt Template from JSON Source: https://github.com/patterns-ai-core/langchainrb/wiki/1.-Prompts Load a FewShotPromptTemplate from a JSON file using Langchain::Prompt.load_from_path. Access properties like prefix after loading. ```ruby prompt = Langchain::Prompt.load_from_path(file_path: "spec/fixtures/prompt/few_shot_prompt_template.json") prompt.prefix # "Write antonyms for the following words." ``` -------------------------------- ### Generate Completions Source: https://github.com/patterns-ai-core/langchainrb/wiki/2.-LLMs Use the complete method to generate text based on a provided prompt. ```ruby openai.complete(prompt:) ``` -------------------------------- ### LLM Core Methods Source: https://github.com/patterns-ai-core/langchainrb/wiki/2.-LLMs This section describes the core methods available for interacting with instantiated LLMs, including generating embeddings, text completions, chat completions, and content summarization. ```APIDOC ### Methods #### Generating embeddings `embed(text:)` method generates and returns an embedding. ```ruby openai.embed(text: "Some text to embed") ``` #### Generating completion `complete(prompt:)` generates a completion for a given prompt. ```ruby openai.complete(prompt: "Translate this text to French: Hello world") ``` #### Generating chat completion `chat(message:)` generates a chat completion for a given message. ```ruby openai.chat(message: { role: "user", content: "What is the weather like today?" }) ``` #### Summarizing content `summarize(text:)` will create a summary for a given text. This is the [prompt](https://github.com/andreibondarev/langchainrb/blob/main/lib/llm/prompts/summarize_template.json) that will be used. ```ruby openai.summarize(text: "A long article about AI.") ``` ``` -------------------------------- ### Define JSON Schema and Create Structured Output Parser Source: https://github.com/patterns-ai-core/langchainrb/blob/main/README.md Define a JSON schema for the desired output and use it to create a StructuredOutputParser. This parser will generate format instructions for the LLM. ```ruby json_schema = { type: "object", properties: { name: { type: "string", description: "Persons name" }, age: { type: "number", description: "Persons age" }, interests: { type: "array", items: { type: "object", properties: { interest: { type: "string", description: "A topic of interest" }, levelOfInterest: { type: "number", description: "A value between 0 and 100 of how interested the person is in this interest" } }, required: ["interest", "levelOfInterest"], additionalProperties: false }, minItems: 1, maxItems: 3, description: "A list of the person's interests" } }, required: ["name", "age", "interests"], additionalProperties: false } parser = Langchain::OutputParsers::StructuredOutputParser.from_json_schema(json_schema) prompt = Langchain::Prompt::PromptTemplate.new(template: "Generate details of a fictional character.\n{format_instructions}\nCharacter description: {description}", input_variables: ["description", "format_instructions"]) prompt_text = prompt.format(description: "Korean chemistry student", format_instructions: parser.get_format_instructions) ``` -------------------------------- ### Execute Agent Query Source: https://github.com/patterns-ai-core/langchainrb/wiki/7.-Experimental Run a question through the agent to receive a generated response. ```ruby agent.run(question: "How many full soccer fields would be needed to cover the distance between NYC and DC in a straight line?") #=> "Approximately 2,945 soccer fields would be needed to cover the distance between NYC and DC in a straight line." ``` -------------------------------- ### Generate Prompt Completions Source: https://github.com/patterns-ai-core/langchainrb/blob/main/README.md Generate text completions based on a provided prompt. ```ruby response = llm.complete(prompt: "Once upon a time") completion = response.completion ``` -------------------------------- ### Save and Load Prompt Templates Source: https://github.com/patterns-ai-core/langchainrb/blob/main/README.md Persist prompt templates to JSON or YAML files and load them back into the application. ```ruby prompt.save(file_path: "spec/fixtures/prompt/prompt_template.json") ``` ```ruby prompt = Langchain::Prompt.load_from_path(file_path: "spec/fixtures/prompt/prompt_template.json") prompt.input_variables # ["adjective", "content"] ``` ```ruby prompt = Langchain::Prompt.load_from_path(file_path: "spec/fixtures/prompt/prompt_template.yaml") prompt.input_variables #=> ["adjective", "content"] ``` -------------------------------- ### Require Langchain.rb Source: https://github.com/patterns-ai-core/langchainrb/wiki/Home Load the library into your Ruby application. ```ruby require "langchain" ``` -------------------------------- ### Create Default Schema in Weaviate Source: https://github.com/patterns-ai-core/langchainrb/wiki/5.-Vector-Databases Use `create_default_schema` to set up the default schema in your Weaviate vector database. Customizable schema creation is planned for future releases. ```ruby weaviate.create_default_schema ``` -------------------------------- ### Configure Langchain.rb Log Destination Source: https://github.com/patterns-ai-core/langchainrb/blob/main/README.md Redirect Langchain.rb logs from the default STDOUT to a specified file path. This allows for persistent logging to a file. ```ruby Langchain.logger = Logger.new("path/to/file", **Langchain::LOGGER_OPTIONS) ``` -------------------------------- ### chat() Method Parameters Source: https://github.com/patterns-ai-core/langchainrb/blob/main/README.md Details the parameters accepted by the chat() method for generating chat completions. ```APIDOC ## chat() Method ### Description Generates a chat completion based on the provided conversation history and configuration parameters. ### Parameters #### Request Body - **messages** (array) - Required - An array of message objects representing the conversation history. - **model** (string) - Optional - The specific chat model to use. - **temperature** (float) - Optional - Controls randomness in generation. - **top_p** (float) - Optional - An alternative to temperature, controls diversity of generated tokens. - **n** (integer) - Optional - Number of chat completion choices to generate. - **max_tokens** (integer) - Optional - The maximum number of tokens to generate in the chat completion. - **stop** (array/string) - Optional - Sequences where the API will stop generating further tokens. - **presence_penalty** (float) - Optional - Penalizes new tokens based on their presence in the text so far. - **frequency_penalty** (float) - Optional - Penalizes new tokens based on their frequency in the text so far. - **logit_bias** (map) - Optional - Modifies the likelihood of specified tokens appearing in the completion. - **user** (string) - Optional - A unique identifier representing your end-user. - **tools** (array) - Optional - A list of tools the model may call. - **tool_choice** (string) - Optional - Controls how the model calls functions. ``` -------------------------------- ### Add Data Using File Parsers Source: https://github.com/patterns-ai-core/langchainrb/blob/main/README.md Loads, parses, and indexes data from various file types into your vector search database using Langchain's file parsers. Supported formats include docx, html, pdf, text, json, jsonl, csv, xlsx, eml, pptx. ```ruby my_pdf = Langchain.root.join("path/to/my.pdf") my_text = Langchain.root.join("path/to/my.txt") my_docx = Langchain.root.join("path/to/my.docx") client.add_data(paths: [my_pdf, my_text, my_docx]) ``` -------------------------------- ### Langchain::Processors::HTML Source: https://github.com/patterns-ai-core/langchainrb/wiki/4.-Preparing-Data Processes HTML files. Requires the 'nokogiri' gem. ```APIDOC ## Langchain::Processors::HTML ### Description Processes HTML files. ### Gem Requirements `gem "nokogiri", ">~ 1.13"` ``` -------------------------------- ### Similarity Search by Query Source: https://github.com/patterns-ai-core/langchainrb/blob/main/README.md Retrieves documents similar to the provided query string. Specify the number of results (k) to be returned. ```ruby client.similarity_search( query:, k: # number of results to be retrieved ) ``` -------------------------------- ### Schema Creation Source: https://github.com/patterns-ai-core/langchainrb/wiki/5.-Vector-Databases This method allows for the creation of a default schema within your chosen vector database. ```APIDOC ## Schema Creation `create_default_schema()` creates default schema in your vector database. ```ruby weaviate.create_default_schema ``` (We plan on offering customizable schema creation shortly) ``` -------------------------------- ### Save Prompt Template to JSON Source: https://github.com/patterns-ai-core/langchainrb/wiki/1.-Prompts Save a PromptTemplate object to a JSON file using the save method, specifying the file path. ```ruby prompt.save(file_path: "spec/fixtures/prompt/prompt_template.json") ``` -------------------------------- ### Simplified Output Fixing Parser Usage Source: https://github.com/patterns-ai-core/langchainrb/blob/main/README.md If you do not need to explicitly handle the OutputParserException, you can directly use the OutputFixingParser to parse the LLM response, which will automatically attempt to fix parsing errors. ```ruby fix_parser = Langchain::OutputParsers::OutputFixingParser.from_llm( llm: llm, parser: parser ) fix_parser.parse(llm_response) ``` -------------------------------- ### Generate Chat Completions Source: https://github.com/patterns-ai-core/langchainrb/wiki/2.-LLMs Use the chat method to generate conversational responses. ```ruby openai.chat(message:) ``` -------------------------------- ### Adding Data Source: https://github.com/patterns-ai-core/langchainrb/wiki/5.-Vector-Databases Methods for adding data to the vector database, supporting both file paths and direct text input. ```APIDOC ## Adding Data You can add data with: 1. `add_data(path:, paths:)` to add any kind of data type ```ruby my_pdf = Langchain.root.join("path/to/my.pdf") my_text = Langchain.root.join("path/to/my.txt") my_docx = Langchain.root.join("path/to/my.docx") my_csv = Langchain.root.join("path/to/my.csv") weaviate.add_data(paths: [my_pdf, my_text, my_docx, my_csv]) ``` 2. `add_texts(texts:)` to only add textual data ```ruby client.add_texts( texts: [ "Lorem Ipsum is simply dummy text of the printing and typesetting industry.", "Lorem Ipsum has been the industry's standard dummy text ever since the 1500s" ] ) ``` ``` -------------------------------- ### POST /chat Source: https://github.com/patterns-ai-core/langchainrb/blob/main/README.md Generates chat completions based on a sequence of messages. ```APIDOC ## POST /chat ### Description Generates a chat completion based on a list of messages representing a conversation history. ### Method POST ### Parameters #### Request Body - **messages** (array) - Required - A list of message objects containing 'role' and 'content' (or 'parts' for specific providers). ### Response #### Success Response (200) - **chat_completion** (string) - The generated chat response. ``` -------------------------------- ### Langchain::Processors::JSONL Source: https://github.com/patterns-ai-core/langchainrb/wiki/4.-Preparing-Data Processes JSONL files. No specific gem requirements. ```APIDOC ## Langchain::Processors::JSONL ### Description Processes JSONL files. ### Gem Requirements None ``` -------------------------------- ### Save Few-Shot Prompt Template to JSON Source: https://github.com/patterns-ai-core/langchainrb/wiki/1.-Prompts Save a FewShotPromptTemplate object to a JSON file using the save method, specifying the file path. ```ruby prompt.save(file_path: "spec/fixtures/prompt/few_shot_prompt_template.json") ``` -------------------------------- ### Langchain::Processors::Xlsx Source: https://github.com/patterns-ai-core/langchainrb/wiki/4.-Preparing-Data Processes XLSX files. Requires the 'roo' gem. ```APIDOC ## Langchain::Processors::Xlsx ### Description Processes XLSX files. ### Gem Requirements `gem "roo", ">~ 2.10.0"` ``` -------------------------------- ### Langchain::Processors::Docx Source: https://github.com/patterns-ai-core/langchainrb/wiki/4.-Preparing-Data Processes DOCX files. Requires the 'docx' gem. ```APIDOC ## Langchain::Processors::Docx ### Description Processes DOCX files. ### Gem Requirements `gem "docx", ">~ 0.8.0"` ``` -------------------------------- ### Langchain::Processors::PDF Source: https://github.com/patterns-ai-core/langchainrb/wiki/4.-Preparing-Data Processes PDF files. Requires the 'pdf-reader' gem. ```APIDOC ## Langchain::Processors::PDF ### Description Processes PDF files. ### Gem Requirements `gem "pdf-reader", ">~ 1.4"` ``` -------------------------------- ### POST /embed Source: https://github.com/patterns-ai-core/langchainrb/blob/main/README.md Generates vector embeddings for a given input text using the configured LLM provider. ```APIDOC ## POST /embed ### Description Generates embeddings for the provided input text. ### Method POST ### Parameters #### Request Body - **text** (string) - Required - The input text to embed. - **model** (string) - Optional - The specific model name to use for embedding. ### Response #### Success Response (200) - **embedding** (array) - The generated vector embedding. ``` -------------------------------- ### Langchain::Processors::Text Source: https://github.com/patterns-ai-core/langchainrb/wiki/4.-Preparing-Data Processes plain text files. No specific gem requirements. ```APIDOC ## Langchain::Processors::Text ### Description Processes plain text files. ### Gem Requirements None ``` -------------------------------- ### Langchain::Processors::CSV Source: https://github.com/patterns-ai-core/langchainrb/wiki/4.-Preparing-Data Processes CSV files. Requires the 'docx' gem. ```APIDOC ## Langchain::Processors::CSV ### Description Processes CSV files. ### Gem Requirements `gem "docx", ">~ 0.8.0"` ``` -------------------------------- ### Handle Parsing Errors with OutputFixingParser Source: https://github.com/patterns-ai-core/langchainrb/blob/main/README.md If the LLM's response cannot be parsed by the StructuredOutputParser, use the OutputFixingParser. This parser will attempt to fix the response by sending it back to the LLM with error details. ```ruby begin parser.parse(llm_response) rescue Langchain::OutputParsers::OutputParserException => e fix_parser = Langchain::OutputParsers::OutputFixingParser.from_llm( llm: llm, parser: parser ) fix_parser.parse(llm_response) end ``` -------------------------------- ### Similarity Search by Query Source: https://github.com/patterns-ai-core/langchainrb/wiki/5.-Vector-Databases Search the vector database for documents similar to a given query string. This method first generates an embedding for the query and then performs a similarity search. ```ruby client.similarity_search( query:, k: ) ``` -------------------------------- ### Generate Chat Completions Source: https://github.com/patterns-ai-core/langchainrb/blob/main/README.md Send a series of messages to the LLM to generate a chat response. ```ruby messages = [ { role: "system", content: "You are a helpful assistant." }, { role: "user", content: "What's the weather like today?" } # Google Gemini and Google VertexAI expect messages in a different format: # { role: "user", parts: [{ text: "why is the sky blue?" }]} ] response = llm.chat(messages: messages) chat_completion = response.chat_completion ``` -------------------------------- ### Add Weaviate Gem Dependency Source: https://github.com/patterns-ai-core/langchainrb/blob/main/README.md Add the Weaviate Ruby gem to your project's Gemfile to use Weaviate as a vector search database. ```ruby gem "weaviate-ruby", "~> 0.8.9" ``` -------------------------------- ### Summarize Content Source: https://github.com/patterns-ai-core/langchainrb/wiki/2.-LLMs Use the summarize method to generate a summary of the provided text. ```ruby openai.summarize(text:) ``` -------------------------------- ### RAG-based Querying Source: https://github.com/patterns-ai-core/langchainrb/blob/main/README.md Executes a question against the indexed data using a Retrieval-Augmented Generation (RAG) approach. This combines retrieval of relevant information with language model generation to answer the question. ```ruby client.ask(question: "...") ``` -------------------------------- ### Langchain::Processors::JSON Source: https://github.com/patterns-ai-core/langchainrb/wiki/4.-Preparing-Data Processes JSON files. No specific gem requirements. ```APIDOC ## Langchain::Processors::JSON ### Description Processes JSON files. ### Gem Requirements None ``` -------------------------------- ### Set Langchain.rb Logger Level Source: https://github.com/patterns-ai-core/langchainrb/blob/main/README.md Configure the logging level for Langchain.rb to DEBUG to display all log messages. This is useful for detailed debugging. ```ruby Langchain.logger.level = Logger::DEBUG ``` -------------------------------- ### Similarity Search by Vector Source: https://github.com/patterns-ai-core/langchainrb/blob/main/README.md Retrieves documents similar to a given embedding vector. Specify the number of results (k) to be returned. ```ruby client.similarity_search_by_vector( embedding:, k: # number of results to be retrieved ) ``` -------------------------------- ### Similarity Search with HyDE Source: https://github.com/patterns-ai-core/langchainrb/blob/main/README.md Performs a similarity search using the Hypothetical Document Embeddings (HyDE) technique. This method generates a hypothetical answer to the query first and then uses its embedding for the search. ```ruby client.similarity_search_with_hyde() ``` -------------------------------- ### Generate Embeddings Source: https://github.com/patterns-ai-core/langchainrb/wiki/2.-LLMs Use the embed method to generate vector representations of text. ```ruby openai.embed(text:) ``` -------------------------------- ### Add Plain Text Data Source: https://github.com/patterns-ai-core/langchainrb/blob/main/README.md Adds an array of plain text strings to the vector search database. Each string will be embedded and indexed. ```ruby client.add_texts( texts: [ "Begin by preheating your oven to 375°F (190°C). Prepare four boneless, skinless chicken breasts by cutting a pocket into the side of each breast, being careful not to cut all the way through. Season the chicken with salt and pepper to taste. In a large skillet, melt 2 tablespoons of unsalted butter over medium heat. Add 1 small diced onion and 2 minced garlic cloves, and cook until softened, about 3-4 minutes. Add 8 ounces of fresh spinach and cook until wilted, about 3 minutes. Remove the skillet from heat and let the mixture cool slightly.", "In a bowl, combine the spinach mixture with 4 ounces of softened cream cheese, 1/4 cup of grated Parmesan cheese, 1/4 cup of shredded mozzarella cheese, and 1/4 teaspoon of red pepper flakes. Mix until well combined. Stuff each chicken breast pocket with an equal amount of the spinach mixture. Seal the pocket with a toothpick if necessary. In the same skillet, heat 1 tablespoon of olive oil over medium-high heat. Add the stuffed chicken breasts and sear on each side for 3-4 minutes, or until golden brown." ] ) ``` -------------------------------- ### Similarity Search by Embedding Source: https://github.com/patterns-ai-core/langchainrb/wiki/5.-Vector-Databases Perform a similarity search using a pre-computed embedding. The `similarity_search_by_vector` method retrieves the `k` most similar embeddings to the provided one. ```ruby client.similarity_search_by_vector( embedding:, k: # number of results to be retrieved ) ``` -------------------------------- ### Retrieving Data Source: https://github.com/patterns-ai-core/langchainrb/wiki/5.-Vector-Databases Methods for retrieving data from the vector database using embeddings or natural language queries. ```APIDOC ## Retrieving Data `similarity_search_by_vector(embedding:, k:)` searches the vector database for the closest `k` number of embeddings. ```ruby client.similarity_search_by_vector( embedding:, k: # number of results to be retrieved ) ``` `client.similarity_search(query:, k:)` generates an embedding for the query and searches the vector database for the closest `k` number of embeddings. ```ruby client.similarity_search_by_vector( embedding:, k: # number of results to be retrieved ) ``` `ask(question:)` generates an embedding for the passed-in question, searches the vector database for closest embeddings and then passes these as context to the LLM to generate an answer to the question. ```ruby client.ask( question: ) ``` ``` -------------------------------- ### Add Textual Data Source: https://github.com/patterns-ai-core/langchainrb/wiki/5.-Vector-Databases Use `add_texts` to add only textual data to the vector database. This method is suitable when you have plain text content to index. ```ruby client.add_texts( texts: [ "Lorem Ipsum is simply dummy text of the printing and typesetting industry.", "Lorem Ipsum has been the industry's standard dummy text ever since the 1500s" ] ) ``` -------------------------------- ### Generate Embeddings Source: https://github.com/patterns-ai-core/langchainrb/blob/main/README.md Generate vector embeddings for a specific string of text. ```ruby response = llm.embed(text: "Hello, world!") embedding = response.embedding ```