Skip to content

Instantly share code, notes, and snippets.

@mattlindsey
Last active December 6, 2023 02:23
Show Gist options
  • Save mattlindsey/5f6388d6ff76c2decdccb723bb4ed4c5 to your computer and use it in GitHub Desktop.
Save mattlindsey/5f6388d6ff76c2decdccb723bb4ed4c5 to your computer and use it in GitHub Desktop.
Langchainrb Getting Started Notebook
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"id": "c2998bfe",
"metadata": {},
"source": [
"# Langchainrb Getting Started Notebook\n",
"\n",
"Before we get started, install Jupyter Notedbooks and support for Ruby [here](https://github.com/SciRuby/iruby#readme), and remember to use the command `jupyter notebook` instead of older `iruby notebook` to start the notebook server. VSCode with Jupyter Extensions can also be used.\n",
"\n",
"You should have created and edited .env with the required KEYS, and make sure you have installed the required gems for these examples on the command line:\n",
"\n",
"```bash\n",
"gem install langchainrb\n",
"gem install dotenv\n",
"\n",
"gem install iruby\n",
"iruby register --force\n",
"```\n",
"\n",
"\n",
"Or if you are running from the langchainrb project directory cloned from Github you must add this to the Gemfile:\n",
"```ruby\n",
"gem \"iruby\"\n",
"```\n",
"Then install it on the command line:\n",
"```bash\n",
"bundle install\n",
"iruby register --force\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ead31eb9",
"metadata": {},
"outputs": [],
"source": [
"# setup code for all examples below\n",
"require \"dotenv/load\"\n",
"require \"langchain\""
]
},
{
"cell_type": "markdown",
"id": "993d6b0d",
"metadata": {},
"source": [
"## OpenAI Standalone Example Required Setup\n",
"For the first example, install the following additional gems on the command line:\n",
"\n",
"```bash\n",
"gem install ruby-openai\n",
"```\n",
"and make sure you have defined the KEYS required for this example in your .env file:\n",
"\n",
"```bash\n",
"OPENAI_API_KEY\n",
" ```"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "dc7cb0eb",
"metadata": {},
"outputs": [],
"source": [
"# setup code for below\n",
"require \"openai\""
]
},
{
"cell_type": "markdown",
"id": "12e4d6e2",
"metadata": {},
"source": [
"## OpenAI Standalone Example\n",
"Here is a basic example of using an LLM's methods directly"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "34cd41af",
"metadata": {},
"outputs": [],
"source": [
"openai = Langchain::LLM::OpenAI.new(api_key: ENV.fetch(\"OPENAI_API_KEY\"))\n",
"\n",
"openai.complete(prompt: \"What is the meaning of life?\")"
]
},
{
"cell_type": "markdown",
"id": "bff094cd",
"metadata": {},
"source": [
"## ReAct Agent Example Required Setup\n",
"\n",
"For the first Agent example, install the following additional gems on the command line:\n",
"\n",
"```bash\n",
"gem install ruby-openai\n",
"gem install eqn\n",
"```\n",
"and make sure you have defined the KEYS required for this example in your .env file:\n",
"\n",
"```bash\n",
"OPENAI_API_KEY\n",
" ```"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1c4eb913-1e15-49a9-9b35-d4e21e5adfef",
"metadata": {},
"outputs": [],
"source": [
"# setup for code below\n",
"require \"openai\"\n",
"require \"eqn\""
]
},
{
"cell_type": "markdown",
"id": "13f664dd-1403-4a12-a6c7-d8a18287ce47",
"metadata": {},
"source": [
"## ReAct Agent example using a Tool\n",
"Here is an example of an Agent using a Tool to answer a question by decomposing it into multiple intermediate steps.\n",
"The Calculator Tool solves equations that the LLM may not be equipped to answer."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9915ff7c-dc15-4174-b92e-9e1dd317c0b6",
"metadata": {},
"outputs": [],
"source": [
"Langchain.logger.level = :info\n",
"\n",
"calculator_tool = Langchain::Tool::Calculator.new\n",
"\n",
"openai = Langchain::LLM::OpenAI.new(api_key: ENV.fetch(\"OPENAI_API_KEY\"))\n",
"\n",
"agent = Langchain::Agent::ReActAgent.new(\n",
" llm: openai,\n",
" tools: [calculator_tool]\n",
")\n",
"\n",
"agent.run(question: \"What is 2*2?\")"
]
},
{
"cell_type": "markdown",
"id": "1ad94e55",
"metadata": {},
"source": [
"## ReAct Agent example using Multiple Tools Required Setup\n",
"Here is an example of an Agent using several Tools to answer a question by decomposing it into multiple intermediate steps.\n",
"For this example, install the following additional gems on the command line:\n",
"\n",
"```bash\n",
"gem install ruby-openai\n",
"gem install eqn\n",
"gem install google_search_results\n",
"```\n",
"and make sure you have defined the KEYS required for this example in your .env file:\n",
"\n",
"```bash\n",
"OPENAI_API_KEY\n",
"SERPAPI_API_KEY\n",
" ```"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7ab7206d",
"metadata": {},
"outputs": [],
"source": [
"# setup for code below\n",
"require \"google_search_results\"\n",
"require \"eqn\""
]
},
{
"cell_type": "markdown",
"id": "359b7dcb",
"metadata": {},
"source": [
"## ReAct Agent example using Multiple Tools\n",
"Here is an example of an Agent using Multiple Tools to answer a question by decomposing it into multiple intermediate steps.\n",
"The GooglSearch tool finds current information on Google, and the Calculator tool solves equations."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "913538ca",
"metadata": {},
"outputs": [],
"source": [
"Langchain.logger.level = :info\n",
"\n",
"search_tool = Langchain::Tool::GoogleSearch.new(api_key: ENV.fetch(\"SERPAPI_API_KEY\"))\n",
"calculator_tool = Langchain::Tool::Calculator.new\n",
"\n",
"openai = Langchain::LLM::OpenAI.new(api_key: ENV.fetch(\"OPENAI_API_KEY\"))\n",
"\n",
"agent = Langchain::Agent::ReActAgent.new(\n",
" llm: openai,\n",
" tools: [search_tool, calculator_tool]\n",
")\n",
"\n",
"agent.run(question: \"How many full soccer fields would be needed to cover the distance between NYC and DC in a straight line?\")"
]
},
{
"cell_type": "markdown",
"id": "472da087",
"metadata": {},
"source": [
"## SQLQuery Agent Example Required Setup¶\n",
"For this example, install the following additional gems on the command line:\n",
"\n",
"```bash\n",
"gem install ruby-openai\n",
"gem install sequel\n",
"```\n",
"and make sure you have defined the KEYS required for this example in your .env file:\n",
"```bash\n",
"OPENAI_API_KEY\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5add5444",
"metadata": {},
"outputs": [],
"source": [
"# setup for code below\n",
"require 'sequel'"
]
},
{
"cell_type": "markdown",
"id": "4321bd63",
"metadata": {},
"source": [
"## SQLQueryAgent example\n",
"Here is an example of using the Database Tool to connect to a database and ask a question about the data in it.\n",
"Substitute the `connection_string` below with your database information."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d86a612e",
"metadata": {},
"outputs": [],
"source": [
"Langchain.logger.level = :info\n",
"\n",
"database = Langchain::Tool::Database.new(connection_string: \"postgres://postgres:postgres@localhost:5432/db_name\")\n",
"\n",
"agent = Langchain::Agent::SQLQueryAgent.new(llm: Langchain::LLM::OpenAI.new(api_key: ENV[\"OPENAI_API_KEY\"]), db: database)\n",
"\n",
"agent.run(question: \"How many users have a name with length greater than 5 in the users table?\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Ruby 3.2.1",
"language": "ruby",
"name": "ruby"
},
"language_info": {
"file_extension": ".rb",
"mimetype": "application/x-ruby",
"name": "ruby",
"version": "3.2.1"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment