Skip to content

Instantly share code, notes, and snippets.

@shibanovp
Created April 17, 2023 09:10
Show Gist options
  • Save shibanovp/216ce160383369ab83e34377a464d2a1 to your computer and use it in GitHub Desktop.
Save shibanovp/216ce160383369ab83e34377a464d2a1 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%pip install langchain openai"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"os.environ[\"OPENAI_API_KEY\"] = \"\""
]
},
{
"cell_type": "code",
"execution_count": 86,
"metadata": {},
"outputs": [],
"source": [
"from langchain.llms import OpenAI"
]
},
{
"cell_type": "code",
"execution_count": 87,
"metadata": {},
"outputs": [],
"source": [
"llm = OpenAI()"
]
},
{
"cell_type": "code",
"execution_count": 88,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
"1. Neuschwanstein Castle\n",
"2. Berlin Wall\n",
"3. Brandenburg Gate\n",
"4. Cologne Cathedral\n",
"5. Black Forest\n"
]
}
],
"source": [
"prompt = \"List 5 must see places in Germany\"\n",
"print(llm(prompt))"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## Using multiple LLMs with the same prompt."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%pip install aleph-alpha-client"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from langchain.llms import AlephAlpha"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"os.environ[\"ALEPH_ALPHA_API_KEY\"] = \"\""
]
},
{
"cell_type": "code",
"execution_count": 89,
"metadata": {},
"outputs": [],
"source": [
"llms = {\n",
" \"OpenAI\": llm,\n",
" \"AlephAlpha\": AlephAlpha(\n",
" temperature=0.25,\n",
" maximum_tokens=64,\n",
" frequency_penalty=0.4,\n",
" presence_penalty=0.4,\n",
" use_multiplicative_presence_penalty=True,\n",
" ),\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 90,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"model='OpenAI' \n",
"\n",
"A large language model is a type of artificial intelligence (AI) algorithm that is able to generate text based on a large amount of data. It is trained on large corpora of text and is able to generate new text that is similar to the training data. These models are often used in natural language processing (NLP) tasks such as text generation, machine translation, and question answering.\n",
"\n",
"model='AlephAlpha' \n",
"A large language model is a set of features that are used to represent the input data. The more complex and larger your feature space, then you have an increased ability for generalisation (i e., better performance on unseen inputs). In this article we will explore how these models can be trained using TensorFlow\n",
"\n"
]
}
],
"source": [
"prompt = \"What is a large language model?\"\n",
"for model, lm in llms.items():\n",
" print(f\"{model=} {lm(prompt)}\\n\")"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## Prompt Templates\n",
"They allow you to pass a variable or user input."
]
},
{
"cell_type": "code",
"execution_count": 91,
"metadata": {},
"outputs": [],
"source": [
"from langchain.prompts import PromptTemplate"
]
},
{
"cell_type": "code",
"execution_count": 92,
"metadata": {},
"outputs": [],
"source": [
"prompt = PromptTemplate(\n",
" input_variables=[\"city\", \"hours\"],\n",
" template=\"What to do in {city} if I have {hours} spare hours?\",\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 93,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
"1. Visit the East Side Gallery: The East Side Gallery is a 1.3km long section of the Berlin Wall that has been transformed into an open-air gallery featuring over 100 graffiti artworks.\n",
"\n",
"2. Take a boat tour on the River Spree: Enjoy views of the city from the water and see some of Berlin’s most iconic landmarks such as the Reichstag, the Brandenburg Gate, Museum Island and more.\n",
"\n",
"3. Check out Checkpoint Charlie: This famous border crossing between East and West Berlin was once the home of the Cold War. Today, it is a popular tourist spot with its historical museum and the original guardhouse.\n",
"\n",
"4. Explore the Berlin Wall Memorial: This memorial commemorates the victims of the Berlin Wall and includes a museum, an information center, and a series of outdoor installations.\n",
"\n",
"5. Have a beer at the oldest beer garden in Berlin: Located in the Tiergarten, the Prater Biergarten is one of the oldest beer gardens in the city. Enjoy a cold beer and some traditional German cuisine in a relaxed outdoor setting.\n",
"\n",
"6. Take a stroll through the Tiergarten: The Tiergarten is a large park located in the center of\n"
]
}
],
"source": [
"print(llm(prompt.format(city=\"Berlin\", hours=\"4\")))"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## Creating a chain\n",
"They allow you to create a single application from multiple components."
]
},
{
"cell_type": "code",
"execution_count": 94,
"metadata": {},
"outputs": [],
"source": [
"from langchain.chains import LLMChain"
]
},
{
"cell_type": "code",
"execution_count": 95,
"metadata": {},
"outputs": [],
"source": [
"chain = LLMChain(llm=llm, prompt=prompt)"
]
},
{
"cell_type": "code",
"execution_count": 96,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
"1. Explore the Latin Quarter: Take a leisurely stroll through the Latin Quarter, from the Pantheon to the Luxembourg Gardens. You'll find plenty of cafes and restaurants to explore, as well as the lively Place de la Contrescarpe.\n",
"\n",
"2. Visit the Eiffel Tower: Take the iconic elevator ride to the top of the Eiffel Tower and admire the incredible views of Paris.\n",
"\n",
"3. Visit the Louvre: Spend a few hours exploring the vast collections of art and antiquities at the world-famous Louvre Museum.\n",
"\n",
"4. Take a Boat Cruise on the Seine: Board a boat and take a leisurely cruise along the River Seine, passing many of Paris's famous landmarks.\n",
"\n",
"5. Go Shopping in the Marais: Browse the boutiques and designer stores in the trendy Marais district, or discover some hidden gems at the local flea markets.\n"
]
}
],
"source": [
"print(chain.run(city=\"Paris\", hours=\"5\"))"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"# Creating an Agent\n",
"Agents can \"think\" and make decisions to achieve specific goals."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%pip install google-search-results"
]
},
{
"cell_type": "code",
"execution_count": 108,
"metadata": {},
"outputs": [],
"source": [
"from langchain.agents import load_tools, initialize_agent, AgentType"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"os.environ[\"SERPAPI_API_KEY\"] = \"\""
]
},
{
"cell_type": "code",
"execution_count": 109,
"metadata": {},
"outputs": [],
"source": [
"tools = load_tools([\"serpapi\", \"llm-math\"], llm=llm)\n",
"agent = initialize_agent(\n",
" tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 110,
"metadata": {},
"outputs": [],
"source": [
"prompt = \"How many days has passed since GPT-4.0 release?\""
]
},
{
"cell_type": "code",
"execution_count": 111,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
"\u001b[32;1m\u001b[1;3m This is a current event question, so I should use a search engine.\n",
"Action: Search\n",
"Action Input: \"GPT-4.0 release date\"\u001b[0m\n",
"Observation: \u001b[36;1m\u001b[1;3mMarch 14, 2023\u001b[0m\n",
"Thought:\u001b[32;1m\u001b[1;3m I now know when GPT-4.0 was released, so I need to calculate the number of days since then.\n",
"Action: Calculator\n",
"Action Input: March 14, 2023 - today's date\u001b[0m\n",
"Observation: \u001b[33;1m\u001b[1;3mAnswer: -33 days, 0:00:00\n",
"\u001b[0m\n",
"Thought:\u001b[32;1m\u001b[1;3m I now know the final answer.\n",
"Final Answer: 33 days have passed since GPT-4.0 release.\u001b[0m\n",
"\n",
"\u001b[1m> Finished chain.\u001b[0m\n"
]
},
{
"data": {
"text/plain": [
"'33 days have passed since GPT-4.0 release.'"
]
},
"execution_count": 111,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"agent.run(prompt)"
]
},
{
"cell_type": "code",
"execution_count": 112,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"datetime.timedelta(days=33)"
]
},
"execution_count": 112,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from datetime import date\n",
"\n",
"date.today() - date(2023, 3, 14)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment