Skip to content
This repository has been archived by the owner on Oct 14, 2024. It is now read-only.

Commit

Permalink
save (#1182)
Browse files Browse the repository at this point in the history
  • Loading branch information
luojiyin1987 authored Oct 11, 2023
1 parent 0a4c976 commit 845086d
Showing 1 changed file with 58 additions and 57 deletions.
115 changes: 58 additions & 57 deletions chinese/articles/create-an-ai-tweet-generator-openai-langchain.md
Original file line number Diff line number Diff line change
@@ -1,50 +1,50 @@
> - 原文地址:[How to Create an AI Tweet Generator Using LangChain](https://www.freecodecamp.org/news/create-an-ai-tweet-generator-openai-langchain/)
> - 原文作者:[Shane Duggan](https://www.freecodecamp.org/news/author/shane/)
> - 译者:
> - 译者:[luojiyin](https://github.com/luojiyin1987)
> - 校对者:
![How to Create an AI Tweet Generator Using LangChain](https://www.freecodecamp.org/news/content/images/size/w2000/2023/06/cover.png)

I've got a fun tutorial for you today. If you've read my previous article on building [custom-knowledge chatbots using LangChain](https://www.freecodecamp.org/news/langchain-how-to-create-custom-knowledge-chatbots/), then you must be bursting with ideas of great projects you can create.
今天我给大家带来了一个有趣的教程。如果你读过我上一篇关于使用 LangChain 构建[自定义知识聊天机器人](https://www.freecodecamp.org/news/langchain-how-to-create-custom-knowledge-chatbots/)的文章,那么你一定对自己可以创建的伟大项目充满了想法。

Well, I want to encourage your creativity and give a concrete example of what you can build using LangChain along with a Large Language Model (LLM). And while it might sound intimidating, it's actually pretty simple to implement.
好的,我想鼓励你的创造力,并举例说明使用 LangChain 和大型语言模型(LLM)可以创建什么。虽然听起来有点吓人,但实际上实现起来非常简单。

Today, we're going to be creating an AI Tweet Generator using LangChain and OpenAI's LLMs. It's a simple project that takes in a Tweet topic and outputs a Tweet about it.
今天,我们将使用 LangChain OpenAI 的 LLM 创建一个 AI Tweet 生成器。这是一个简单的项目,它接收一个 Tweet 主题并输出相关的 Tweet。

But what's so special about that? Well, the fun part is that we are going to use LangChain to be able to **reference up-to-date information** through Wikipedia. This lets us overcome ChatGPT's training data limitation, as it was only trained on data up to 2021.
这有什么特别的?有趣的是,我们将使用 LangChain 通过维基百科上的最新信息。这让我们克服了 ChatGPT 的训练数据限制,因为它只能在 2021 年之前的数据上进行训练。

This is what we'll be making:
这就是我们要做的:

![B-AqnuHPFtkT010tllL0VZlbZRK-wasEjUwl8a5yzDRCuG3VYRt8hz1QPC3tz1F_vnDSXwHM8gJNIbM9jFcGbnz1uu4OSQB-hTVSuDYULlfVRWlQfewvFpS4-XF8pkMn37Gu5Au4liSxujehfV7uCWg](https://lh4.googleusercontent.com/B-AqnuHPFtkT010tllL0VZlbZRK-wasEjUwl8a5yzDRCuG3VYRt8hz1QPC3tz1F_vnDSXwHM8gJNIbM9jFcGbnz1uu4OSQB-hTVSuDYULlfVRWlQfewvFpS4-XF8pkMn37Gu5Au4liSxujehfV7uCWg)

Take a look at the information referenced by our Tweet below. It uses Wikipedia information regarding Microsoft's investment into OpenAI **in 2023**. So now, you won't need to worry about the data your AI is referencing being outdated.
请看下面我们的推特所引用的信息。它使用了维基百科关于微软在 **2023** 对 OpenAI 进行投资的信息。因此,现在您不必担心您的人工智能所引用的数据会过时了。

![r5CxHduLOViifkCaI2R84nl-n26rGVHnJCOa3Rgpt_WqXlyL9O7Hnar52p0yGLhKNhe3F5F3X6CNM98-0oJBeBXQ8IvQvNgTZirblgs5lSU4j8G9X_X1ROgoPd06vIGhLd_mdmWyEZzAtrC5ESSXvZA](https://lh6.googleusercontent.com/r5CxHduLOViifkCaI2R84nl-n26rGVHnJCOa3Rgpt_WqXlyL9O7Hnar52p0yGLhKNhe3F5F3X6CNM98-0oJBeBXQ8IvQvNgTZirblgs5lSU4j8G9X_X1ROgoPd06vIGhLd_mdmWyEZzAtrC5ESSXvZA)

If that sounds good to you, then let's jump in.
如果你觉得听起来不错,那我们就开始吧。

## How to Set Up the Project
## 如何设置项目

While this project is going to require several components, everything actually sits nicely in single app.py file that simply brings together several APIs.
虽然这个项目需要多个组件,但实际上所有内容都可以很好地整合到一个 app.py 文件中,该文件只需将多个 API 整合在一起即可。

Structurally, we'll just create the app.py file and an apikey.py file to store our API keys (mainly for [OpenAI](https://openai.com/blog/openai-api)).
在结构上,我们只需创建 app.py 文件和 apikey.py 文件来存储 API key (主要用于 [OpenAI](https://openai.com/blog/openai-api))

Along with that, let's get our services installed. Here's the list of the libraries we are going to be using for this project:
此外,我们还要安装的库。以下是我们将在本项目中使用的库列表:

- **Streamlit** – Used to build the app
- **LangChain** – Used to build LLM workflow.
- **OpenAI** – For using OpenAI GPT
- **Wikipedia** – Used to connect GPT to WIKIPEDIA
- **ChromaDB** – Vector storage
- **Tiktoken** – Backend tokenizer for OpenAI
- **Streamlit** – Used to build the app (用于构建应用程序)
- **LangChain** – Used to build LLM workflow. (用于建立 LLM 工作流程)
- **OpenAI** – For using OpenAI GPT (调用 OpenAI GPT 接口)
- **Wikipedia** – Used to connect GPT to WIKIPEDIA (用于连接 GPT 和 WIKIPEDIA)
- **ChromaDB** – Vector storage(向量存储)
- **Tiktoken** – Backend tokenizer for OpenAI (OpenAI 的后台令牌生成器)

要安装这些软件,请在终端运行以下命令:

To install these, run the below comment in your terminal:

```
```shell
pip install streamlit langchain openai wikipedia chromadb tiktoken
```

If your system already contains some of these services then you can install them one by one. Additionally, we'll map the API Key variable for this environment. Let's import these into our app.py file and we should be ready to get started.
如果您的系统已经包含其中一些服务,则可以逐一安装。此外,我们还将为该环境配置 API Key 变量。将这些内容导入 app.py 文件后,我们就可以开始了。

```Python
import os
Expand All @@ -60,27 +60,27 @@ from langchain.utilities import WikipediaAPIWrapper
os.environ['OPENAI_API_KEY'] = apikey
```

## How to Implement the UI
## 如何实现用户界面

Now, the app we are creating is fairly simple. So I've decided to keep things as minimalistic as possible with the UI, with a single title and text input field. For this Tweet generator, it serves its purpose.
现在,我们要创建的应用程序相当简单。因此,我决定尽可能简化用户界面,只使用一个标题和文本输入框。对于这个 Tweet 生成器来说,这已经达到了目的。

```Python
# Creating the title and input field
st.title('🦜🔗 Tweet Generator')
prompt = st.text_input('Tweet topic: ')
```

Later on, we'll be adding in functionality to display our topic history, tweet history, and most importantly, the Wikipedia data we referenced. For now, this is the UI we'll be working with:
稍后,我们将添加功能来显示我们的话题历史、推特历史,以及最重要的,我们引用的维基百科数据。现在,这就是我们要使用的用户界面:

![Screenshot-2023-06-21-at-4.38.00-PM](https://www.freecodecamp.org/news/content/images/2023/06/Screenshot-2023-06-21-at-4.38.00-PM.png)

## How to Include the Prompt Templates
## 如何包含提示模板

Now we're moving into LangChain territory. At this point, if you are unfamiliar with LangChain and haven't read my [previous article](https://www.freecodecamp.org/news/langchain-how-to-create-custom-knowledge-chatbots/) on LangChain, I'd highly recommend doing so the get a better idea of the coming steps.
现在,我们将进入 LangChain 领域。在这一点上,如果你对 LangChain 不熟悉,也没有阅读过我关于 LangChain [上一篇文章](https://www.freecodecamp.org/news/langchain-how-to-create-custom-knowledge-chatbots/),我强烈建议你阅读一下,以便更好地了解接下来的步骤。

The first thing we're going to do is introduce our PromptTemplates. Just to recap, PromptTemplates act as wrappers for your prompts to be able to chain them with multiple operations, which is the foundation of LangChain.
我们要做的第一件事就是介绍 PromptTemplates。概括地说,PromptTemplates 是提示语的封装器,可以通过多种操作将它们串联起来,这也是 LangChain 的基础。

In addition, we'll include a wrapper for the Wikipedia API to allow us to include the data in the chain execution.
此外,我们还将为维基百科应用程序接口(Wikipedia API)提供一个封装,以便在链式执行中包含数据。

```Python
# template for the title
Expand All @@ -99,13 +99,13 @@ tweet_template = PromptTemplate(
wiki = WikipediaAPIWrapper()
```

In this example, I created a title prompt as well, just to give us an overarching title of our Tweet topic. For the actual prompting, if you have been using ChatGPT, it's basically the same concept – it's just that now we are introducing variables (tweet topics).
在这个例子中,我还创建了一个 标题提示(title prompt),为我们的推文主题提供一个总标题。至于实际的提示,如果你一直在使用 ChatGPT,它的概念基本相同,只是现在我们引入了变量(推特话题)。

This allows us to avoid typing "write me a tweet about..." for every single input. Instead, we just need to insert the topic. With that out of the way, let's move into introducing the actual OpenAI LLM.
这样我们就可以避免每次输入时都输入 `给我写一条关于...的推文`。相反,我们只需插入话题即可。说完这些,让我们开始介绍实际的 OpenAI LLM

## Introducing OpenAI's LLMs

There are several ways to do this, and you can choose whichever model you deem fit. In my previous article, I used the GPT-3.5-turbo [chat model](https://python.langchain.com/docs/modules/model_io/models/chat/integrations/openai) with the following code:
有几种方法可以做到这一点,您可以选择自己认为合适的模式。在上一篇文章中,我使用了 GPT-3.5-turbo [chat model](https://python.langchain.com/docs/modules/model_io/models/chat/integrations/openai),代码如下:

```Python
chat = ChatOpenAI(model_name="gpt-3.5-turbo",temperature=0.3)
Expand All @@ -115,49 +115,50 @@ messages = [
]
```

But you can decide which module you'd like to use with your API key and just follow along the [LangChain documentation](https://python.langchain.com/docs/modules/model_io/models/llms/integrations/openai) to get that set up.
不过,您可以根据自己的 API 密钥决定使用哪个模块,然后按照 [LangChain 文档](https://python.langchain.com/docs/modules/model_io/models/llms/integrations/openai) 进行设置即可。

Today, we'll be using the model "text-davinci-003", which is essentially the same GPT-3 model as in the early days of ChatGPT.

今天,我们将使用 "text-davinci-003 "模型,它与 ChatGPT 早期的 GPT-3 模型基本相同。

![Screenshot-2023-06-22-at-12.17.17-PM](https://www.freecodecamp.org/news/content/images/2023/06/Screenshot-2023-06-22-at-12.17.17-PM.png)

OpenAI's various models (Found on their [website](https://platform.openai.com/docs/models/overview))
OpenAI 的各种模型 (见其 [网站](https://platform.openai.com/docs/models/overview))

Feel free to experiment with the models to see which tweets yield the best results. You could even try the significantly more powerful (and expensive) GPT-4, but with such a simple prompt-completion case like a tweet generator it might not be necessary.
您可以随意尝试使用这些模型,看看哪种推文能产生最好的结果。您甚至可以尝试功能更强大(也更昂贵)的 GPT-4,但对于像推文生成器这样简单的提示完成案例,可能就没有必要了。

```Python
llm = OpenAI(model_name="text-davinci-003", temperature=0.9)
title_chain = LLMChain(llm=llm, prompt=title_template, verbose=True, output_key='title', memory=title_memory)
script_chain = LLMChain(llm=llm, prompt=script_template, verbose=True, output_key='script', memory=script_memory)
```

I've also decided to specify the temperature as 0.9 to allow the model to come up with more creative tweets. Temperature acts as a measure of how random and creative the model's responses will be, ranging from 0 for straightforward, and 1 for wildly random. If you'd like yours to be more factual and deterministic, just dial that down.
我还决定将 `temperature` 设定为 0.9,以便让模型发表更有创意的推文。`temperature` 可以衡量模型回复的随机性和创造性,0 代表简单明了,1 代表随机性极强。如果你希望你的回应更符合事实和确定性,只需调低 `temperature` 即可。

Temperature will be the only variable we need to get this going. If you'd like to learn about other fields, take some time to read through the documentation and learn what they are.
`temperature` 将是我们进行这项工作所需的唯一变量。如果你想了解其他字段,请花些时间阅读文档,了解它们的含义。

For example, we can specify token limits to ensure we don't get a long response, but with our current tweet PromptTemplates that should not be an issue.
例如,我们可以指定令牌限制(token limits),以确保不会得到冗长的响应,但使用我们当前的 tweet 提示模板应该不成问题。

## How to Keep Track of Your Tweet Generation History
## 如何跟踪您的 Tweet 生成历史

This is an optional part but provides an additional feature for the app. If you want to keep track of the history of the app activity with information such as previous titles or tweets, then just include the following step:
这是一个可选部分,但可为应用程序提供额外功能。如果您想跟踪应用程序活动的历史记录,包括以前的标题或推文等信息,则只需包含以下步骤即可:

```Python
# Memory
title_memory = ConversationBufferMemory(input_key='topic', memory_key='chat_history')
script_memory = ConversationBufferMemory(input_key='title', memory_key='chat_history')
```

In the code above, we created two different memory variables, `title_memory` and `script_memory`. The `title_memory` keeps the history of tweet topics and the script\_memory keeps the history of tweets.
在上面的代码中,我们创建了两个不同的内存变量:`title_memory` `script_memory``title_memory` 保存推文主题的历史记录,`script_memory` 保存推文的历史记录。

[ConversationBufferMemory](https://python.langchain.com/docs/modules/memory/how_to/buffer) is a feature from LangChain that allows you to keep track of the inputs and outputs given so far (in this case, that's just the topics and tweets we've previously generated).
[ConversationBufferMemory](https://python.langchain.com/docs/modules/memory/how_to/buffer) LangChain 的一项功能,它允许你跟踪迄今为止的输入和输出(在本例中,这只是我们之前生成的话题和推文)。

## How to Chain the Components
## 如何将组件串联起来

Now that we have all of the components of our app sorted (the UI, the prompt templates, and our Wikipedia wrapper), we can put it all together to be executed. And this is where LangChain's value lies.
现在,我们已经对应用程序的所有组件(用户界面、提示模板和维基百科封装器)进行了分类,可以将它们组合在一起执行了。这正是 LangChain 的价值所在。

A good analogy for this would be composite functions in a standard program. Except, our functions in this case are the PromptTemplates, LLMs, and Wikipedia data. Using our wrappers from before, we simply decide the order of execution (like a chain) to get our desired output.
标准程序中的 复合函数(composite functions) 就是一个很好的类比。只不过,这里的函数是 PromptTemplatesLLMs Wikipedia 数据。使用之前的封装器,我们只需决定执行顺序(就像一个链条),就能获得我们想要的输出。

In this case, that would be obtaining the title from our topic, followed by creating the Tweet by using relevant Wikipedia research on the topic, and then displaying those using Streamlit.
在本例中,首先从我们的主题中获取标题,然后使用维基百科对该主题的相关研究创建 Tweet,最后使用 Streamlit 显示这些内容。

```Python
if prompt:
Expand All @@ -178,17 +179,17 @@ if prompt:
st.info(wiki_research) # Storing the Wikipedia Research on the topic to the history
```

When we run those chains, essentially we are taking in the topic from the UI and inserting it into the PromptTemplates that are chained with the LLM. The tweet PromptTemplate also takes in data from Wikipedia to feed to the LLM.
当我们运行 这些链(those chain) 时,基本上是从用户界面获取话题,然后将其插入到与 LLM 相连的 PromptTemplate 中。推文 PromptTemplate 也会从维基百科中获取数据,并输入到 LLM 中。

Finally, it's time to check our app. Run it with the following command:
最后,是时候检查我们的应用程序了。使用以下命令运行它:

```
```shell
streamlit run app.py
```

## Final Code and Where to Go from Here
## 最终代码和下一步行动

The result is being able to overcome the outdated information limitations of ChatGPT and create relevant tweets. If you found it hard to follow along, here's what we have so far:
其结果是能够克服 ChatGPT 过时的信息限制,并创建相关推文。如果您觉得难以理解,下面是我们目前掌握的情况:

```Python
# Importing necessary packages, files and services
Expand Down Expand Up @@ -251,12 +252,12 @@ if prompt:
st.info(wiki_research)
```

Now of course, a Tweet Generator is a simple example of what you can do with LangChain and LLMs. You can apply this same process for creating a YouTube script generator or social media content calendar assistant, for example. The possibilities are endless.
当然,Tweet 生成器只是 LangChain LLMs 的一个简单示例。例如,您还可以应用相同的流程创建 YouTube 脚本生成器或社交媒体内容日历助手。无限可能。

## Conclusion
## 总结

I hope you enjoyed this fun tutorial! LangChain has been getting extremely popular lately and for good reason – it's incredibly versatile. I'd highly recommend getting checking it out.
希望你喜欢这个有趣的教程!LangChain 最近非常流行,这是有原因的--它的用途非常广泛。我强烈推荐你去看看。

If you enjoyed this article and you would like to find out more about the cool new tools AI creators are building, you can stay up-to-date with my [**Byte-Sized AI Newsletter**](https://bytesizedai.beehiiv.com/subscribe). There are tons of exciting stories of what people are building in the AI space, and I'd love for you to join our community.
如果你喜欢这篇文章,并想了解更多关于人工智能创造者正在开发的新工具的信息,你可以通过我的 [**字节大小的人工智能通讯**](https://bytesizedai.beehiiv.com/subscribe) 了解最新信息。这里有大量关于人工智能领域的精彩故事,我希望您能加入我们的社区。

I also post regularly on [Linkedin](https://www.linkedin.com/in/shanepduggan/) and I'd be happy to connect! Other than that, happy building and I'm excited to see what projects you come up with.
我也会定期在 [Linkedin](https://www.linkedin.com/in/shanepduggan/) 上发帖,我很乐意与您联系!除此之外,祝大家建设愉快,我很期待看到你们提出的项目。

0 comments on commit 845086d

Please sign in to comment.