Skip to content
  • Sponsor
  • Notifications You must be signed in to change notification settings
  • Fork 12

Commit 2eaa56c

Browse files
committedJun 17, 2023
feat: add LangChain bots
1 parent 2727ded commit 2eaa56c

File tree

5 files changed

+247
-6
lines changed

5 files changed

+247
-6
lines changed
 

Diff for: ‎12-langchain-bots/README.md

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# LangChain Bots
2+
3+
Two automated AI tools to generate emails and search Wikipedia with LangChain.
4+
5+
## Setup
6+
7+
You need to create a virtual env and install the packages listed in `requirements.txt`.
8+
9+
Follow these steps: [How to Work with Python Virtual Environments, Jupyter Notebooks and VS Code](https://python.plainenglish.io/how-to-work-with-python-virtual-environments-jupyter-notebooks-and-vs-code-536fac3d93a1).
10+
11+
You need to create a `.env` file with your `OPENAI_API_KEY`.
12+
13+
Make sure you have installed `langchain` and `wikipedia`.
14+
15+
## Usage
16+
17+
### Email Generator
18+
19+
To run the tool:
20+
21+
```
22+
cd 12-langchain-bots
23+
python3 email_generator.py
24+
```
25+
26+
Provide the content, the customer name and your name.
27+
28+
### Wikipedia Search Tool
29+
30+
To run the tool:
31+
32+
```
33+
cd 12-langchain-bots
34+
python3 wikipedia_search.py
35+
```
36+
37+
Describe what you need to find.
38+
39+
## Features
40+
41+
- [generating emails](email_generator.py).
42+
- [searching on Wikipedia](wikipedia_search.py).
43+
44+
Based on [ChatGPT - The Complete Guide to ChatGPT & OpenAI APIs](https://www.udemy.com/course/chatgpt-bard-bing-complete-guide-to-chatgpt-openai-apis/) by Maximilian Schwarzmüller (2023).

Diff for: ‎12-langchain-bots/email_generator.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from langchain.llms import OpenAI
2+
from langchain.prompts import PromptTemplate
3+
from dotenv import dotenv_values
4+
5+
config = dotenv_values(".env")
6+
7+
llm = OpenAI(openai_api_key=config["OPENAI_API_KEY"], temperature=0.0)
8+
9+
prompt = PromptTemplate(
10+
input_variables=["content", "customer_name", "agent_name"],
11+
template="""
12+
You are an AI assistant that's optimized to write concise, friendly & professional emails to customers.
13+
14+
Write an email to a customer with the name {customer_name} that contains the following, optimized content: {content}
15+
16+
The email signature should contain the name of the customer agent who wrote the email: {agent_name}
17+
""",
18+
)
19+
20+
content = input("Email content: ")
21+
# sorry about the broken cup, we can replace it or provide a refund
22+
customer = input("Customer name: ")
23+
agent = input("Agent name: ")
24+
25+
response = llm(prompt.format(content=content, customer_name=customer, agent_name=agent))
26+
27+
print(response)
28+
29+
# Dear Gianluca,
30+
31+
# We apologize for the broken cup you received. We would be happy to replace it or provide a refund. Please let us know which option you would prefer.
32+
33+
# Thank you for your patience and understanding.
34+
35+
# Sincerely,
36+
# Mario
37+
# Customer Agent

Diff for: ‎12-langchain-bots/wikipedia_search.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from langchain.llms import OpenAI
2+
from langchain.agents import load_tools, initialize_agent, AgentType
3+
from dotenv import dotenv_values
4+
5+
config = dotenv_values(".env")
6+
7+
llm = OpenAI(openai_api_key=config["OPENAI_API_KEY"], temperature=0.0)
8+
9+
tools = load_tools(["wikipedia", "llm-math"], llm=llm)
10+
# https://python.langchain.com/docs/modules/agents/tools/
11+
12+
agent = initialize_agent(
13+
tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True
14+
)
15+
16+
prompt = input("The Wikipedia research task: ")
17+
18+
agent.run(prompt)
19+
20+
# The Wikipedia research task: How old is President Biden?
21+
22+
23+
# > Entering new chain...
24+
# I need to find out the age of President Biden.
25+
# Action: Wikipedia
26+
# Action Input: Joe Biden
27+
# Observation: Page: Joe Biden
28+
# Summary: Joseph Robinette Biden Jr. ( (listen) BY-dən; born November 20, 1942) is an American politician who is the 46th and current president of the United States. A member of the Democratic Party, he previously served as the 47th vice president from 2009 to 2017 under President Barack Obama and represented Delaware in the United States Senate from 1973 to 2009.
29+
# Born in Scranton, Pennsylvania, Biden moved with his family to Delaware in 1953. He studied at the University of Delaware before earning his law degree from Syracuse University. He was elected to the New Castle County Council in 1970 and became the sixth-youngest senator in U.S. history after he was elected in 1972, at age 29. Biden was the chair or ranking member of the Senate Foreign Relations Committee for 12 years. He chaired the Senate Judiciary Committee from 1987 to 1995; drafted and led the effort to pass the Violent Crime Control and Law Enforcement Act and the Violence Against Women Act; and oversaw six U.S. Supreme Court confirmation hearings, including the contentious hearings for Robert Bork and Clarence Thomas. Biden ran unsuccessfully for the Democratic presidential nomination in 1988 and 2008. Barack Obama chose him as his running mate in the 2008 and 2012 presidential elections. Biden was a close counselor to Obama during his two terms as vice president. He also oversaw infrastructure spending to counteract the Great Recession and had a principal role in designing the withdrawal of United States troops from Iraq. In the 2020 presidential election, Biden and his running mate, Kamala Harris, defeated incumbents Donald Trump and Mike Pence.
30+
# On January 20, 2021, at the age of 78, Biden became the oldest president in U.S. history, the first to have a female vice president, and the first from Delaware. Biden restored America's membership in the Paris Agreement, and signed a $1.9 trillion economic stimulus package in response to the recession caused by the COVID-19 pandemic, a bipartisan bill investing in infrastructure, and the CHIPS and Science Act. Aspects of Biden's Build Back Better Plan were scaled back and presented in the Inflation Reduction Act that was passed by Congress and signed into law by Biden in 2022. The economy during Biden's presidency has been marked by GDP growth and job creation amid significantly elevated inflation. He appointed Ketanji Brown Jackson to the Supreme Court. In 2023, Biden and congressional Republicans prevented a default by negotiating a deal to raise the debt ceiling. In foreign policy, Biden oversaw the withdrawal of U.S. troops from Afghanistan that ended the war in Afghanistan, during which the Afghan government collapsed and the Taliban seized control. He has responded to the Russian invasion of Ukraine by imposing sanctions on Russia and authorizing foreign aid and weapons shipments to Ukraine. On April 25, 2023, Biden announced his intention to run for a second term as president in 2024.
31+
32+
# Page: Family of Joe Biden
33+
# Summary: Joe Biden, the 46th and current president of the United States, has family members who are prominent in law, education, activism and politics. Biden's immediate family became the first family of the United States on his inauguration on January 20, 2021. His immediate family circle was also the second family of the United States from 2009 to 2017, when Biden was vice president. Biden's family is mostly descended from the British Isles, with most of their ancestors coming from Ireland and England, and a smaller number descending from the France.Of Joe Biden's sixteen great-great grandparents, ten were born in Ireland. He is descended from the Blewitts of County Mayo and the Finnegans of County Louth. One of Biden's great-great-great grandfathers was born in Sussex, England, and emigrated to Maryland in the United States by 1820.
34+
35+
# Page: Presidency of Joe Biden
36+
# Summary: Joe Biden's tenure as the 46th president of the United States began with his inauguration on January 20, 2021. Bi
37+
# Thought: I now know the final answer
38+
# Final Answer: Joe Biden is 78 years old.

Diff for: ‎README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# OpenAI Projects
22

3-
11 projects using ChatGPT API, Whisper, Embeddings, and DALL-E with Python.
3+
12 projects using ChatGPT API, Whisper, Embeddings, and DALL-E with Python.
44

55
| # | Project | Description |
66
| --- | --------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------- |
@@ -15,6 +15,7 @@
1515
| 09 | [**Choose Your Own Adventure App**](09-choose-your-own-adventure) | A gamebook app with DALL-E, Stable Diffusion, and GPT-4. |
1616
| 10 | [**Whisper Transcription Tool**](10-whisper-transcription-tool) | A tool to convert spoken audio into accurate transcriptions and translations, using Whisper. |
1717
| 11 | [**Auto-GPT Bots**](11-auto-gpt-bots) | Two AI assistants to scrape emails and teach code with Auto-GPT. |
18+
| 12 | [**LangChain Bots**](12-langchain-bots) | Two automated AI tools to generate emails and search Wikipedia with LangChain. |
1819

1920
## Setup
2021

0 commit comments

Comments
 (0)
Please sign in to comment.