在2024年使用FastAPI创建由LLM驱动的API

FastAPI + OpenAI

在LLM(大语言模型)使得基于AI的应用更加普及的背景下,每个人都希望能够使用像ChatGPT这样的工具来创建下一个炫酷的应用,或者至少满足自己的需求,也许还能在此过程中赚些钱。

在这一系列的文章中,我将向您展示如何使用FastAPI、OpenAI的API和FastCRUD(GitHub仓库在这里)来完成这个任务。以下是本系列文章的索引:

  • Sure, here is the translation of "Part 1. Introduction, Problem Definition, and Setup (you are here)" into simplified Chinese, while keeping the HTML structure intact: ```html 第一部分:介绍,问题定义和设置(您在此处) ```
  • Sure, here is the translation in simplified Chinese while maintaining the HTML structure: ```html

    Part 2: 项目结构,数据库建模和API端点

    ```
  • Sure, here is the text translated into simplified Chinese while maintaining HTML structure: ```html 第三部分:用户功能、认证和安全 ```

Sure, the translation of "Part 1: Introduction, Problem Definition, and Setup" into simplified Chinese while keeping the HTML structure would look like this: ```html

第一部分:介绍、问题定义和设置

```

To translate the given English text into simplified Chinese and keep the HTML structure, you can use the following: ```html

欢迎来到我们关于使用FastAPI、OpenAI API和FastCRUD构建LLM驱动API的系列文章的第一部分。在这个系列中,我们将创建一个个性化电子邮件写作助手,展示如何结合这些技术来构建强大的应用程序。

``` This HTML snippet preserves the structure of the text while providing the translation in simplified Chinese.

Sure, here's the translation of "In this post, we’ll cover:" in simplified Chinese while maintaining the HTML structure: ```html 在这篇文章中,我们将介绍: ```

  • Sure, here's the translated text in simplified Chinese while keeping the HTML structure: ```html

    LLM(语言模型)及其应用介绍。

    ``` In this HTML snippet, `

    ` tags are used to wrap the translated Chinese text, ensuring proper structure.

  • Certainly! Here's the translation of "Tips for picking a good problem to solve with LLMs" in simplified Chinese while keeping the HTML structure: ```html 用LLMs解决问题的好方法选择技巧。 ``` This HTML snippet maintains the structure and translates the text accordingly.
  • Sure, here is the translation of "Evaluating the suitability of an LLM for our example problem" in simplified Chinese, while maintaining the HTML structure: ```html 评估适用于我们示例问题的LLM。 ``` This HTML snippet ensures that the translated Chinese text fits into an HTML context without disrupting the structure.
  • 应用程序的详细要求。
  • 在保持HTML结构的情况下,将以下英文文本翻译为简体中文: 设置项目目录并初始化一个Poetry项目。

To translate "Prerequisites:" into simplified Chinese while keeping the HTML structure, you can use the following: ```html

先决条件:

``` In this HTML snippet: - `

` tags denote a paragraph in HTML. - "先决条件:" is the simplified Chinese translation for "Prerequisites:".

  • To translate "Python 3.9+: Ensure you have Python 3.9 or a newer version installed on your machine." into simplified Chinese while keeping HTML structure, you can use the following: ```html

    Python 3.9+:确保您的计算机上安装了 Python 3.9 或更新的版本。

    ``` In this HTML snippet: - `

    ` and `

    ` are HTML tags for paragraphs. - "Python 3.9+:" is the translated text for "Python 3.9+:" - "确保您的计算机上安装了 Python 3.9 或更新的版本。" is the translation for "Ensure you have Python 3.9 or a newer version installed on your machine."
  • To translate "Poetry: Poetry is a dependency manager for Python. If you don’t have it installed, you can install it by running pip install poetry" into simplified Chinese while keeping the HTML structure intact, you can use the following: ```html

    Poetry: Poetry 是 Python 的依赖管理器。如果您尚未安装它,可以通过运行 pip install poetry 进行安装。

    ```
  • Here is the translated text in simplified Chinese while keeping the HTML structure: ```html

    OpenAI API Key: 您需要从OpenAI获取API密钥才能使用他们的GPT模型。您可以在OpenAI的网站上注册并获取API密钥。

    ```
  • Here's the translated text in simplified Chinese, while keeping the HTML structure: ```html

    FastAPI 基础知识:熟悉 FastAPI 并非必需,但将有助于您更轻松地理解和跟随本教程。

    ```
  • 在维持HTML结构的情况下,将以下英文文本翻译为简体中文: 访问终端(macOS或Linux):您需要运行终端命令来设置项目并管理依赖项。

Sure, here's the translation of "Picking a Problem to Solve" into simplified Chinese while keeping the HTML structure: ```html 选择要解决的问题 ```

Sure, here's how you can translate "Let’s start by picking a problem:" into simplified Chinese while keeping the HTML structure: ```html 让我们从选择一个问题开始: ``` In this translation: - `` is used to maintain the HTML structure. - "让我们" translates to "Let's". - "从选择一个问题开始" translates to "start by picking a problem". Make sure your HTML context allows the use of `` or adjust the tag as needed based on your specific requirements.

```html

要构建一个有用的应用程序,第一步是确定需要解决的问题。即使你开发出了最酷的人工智能应用,如果没有人真正想使用它,那也没有多大意义。以下是选择一个好问题的一些建议:

```

To translate "Scratch Your Own Itch" into simplified Chinese while keeping HTML structure, you can use the following: ```html 抓住自己的痒处 ``` This HTML snippet retains the language attribute for Chinese (simplified) and translates "Scratch Your Own Itch" appropriately.

To translate the given English text into simplified Chinese while maintaining the HTML structure, you can use the following code snippet: ```html

通常,最好解决的问题是你个人经历过的问题。如果你发现某个特定任务具有挑战性或者耗时,那么很可能其他人也有同样的困扰。为自己的问题创建解决方案可以确保你深刻理解问题,并清晰地看到解决方案应该达到的目标。

``` This HTML code will display the translated text in simplified Chinese within a paragraph (`

` tag). Make sure to embed this code snippet appropriately within your HTML document to display the translated text correctly on a webpage.

Sure, here is the translation of "Evaluate Common Challenges" in simplified Chinese while maintaining HTML structure: ```html 评估常见挑战 ``` This translates the phrase directly while preserving the HTML formatting.

在你的社区、工作场所或行业中寻找人们普遍面临的挑战。许多人遇到的问题很可能会吸引更多人寻求解决方案。

在保持HTML结构的前提下,将英文文本"3. Consider Efficiency Gains"翻译为简体中文: 3. 考虑效率提升

Certainly! Here is the text translated to simplified Chinese while keeping the HTML structure: ```html Identify tasks that are repetitive or time-consuming, as these are prime candidates for automation. If automating the task can save significant time and effort, it’s likely a good problem to solve. ``` Translated to simplified Chinese: ```html 识别那些重复或耗时的任务,因为这些是自动化的主要候选项。如果自动化可以节省大量时间和精力,那么这很可能是一个值得解决的好问题。 ```

Certainly! Here's the translation of "Using the guide above, I picked the following example:" into simplified Chinese, while keeping the HTML structure: ```html 使用上述指南,我选择了以下示例: ``` In this HTML snippet, the Chinese text is embedded within the `` structure, maintaining the context of your original request.

问题陈述

在保持HTML结构的情况下,将以下英文文本翻译成简体中文: 写电子邮件可能会耗费大量时间,尤其是对于希望邮件完美无缺的焦虑者而言。无论是专业回复还是随意留言,确保语气和内容恰到好处都是具有挑战性的。这就是个性化电子邮件写作助手发挥作用的地方。

Sure, here's the translation in simplified Chinese, while keeping the HTML structure intact: ```html 要简短一点:写电子邮件通常是耗时且具有挑战性的。 ```

To translate "Now let’s think about whether this is a good fit for an LLM to solve." into simplified Chinese while keeping the HTML structure intact, you can use the following: ```html 现在让我们考虑一下这是否适合LLM来解决。 ``` In this translation: - "现在" means "now". - "让我们" means "let's". - "考虑一下" means "think about". - "这" means "this". - "是否" means "whether". - "适合" means "fit for". - "LLM" remains as "LLM" in Latin letters. - "来解决" means "to solve". Make sure to place this within your HTML structure where appropriate.

To translate "Evaluating if an Application Benefits from an LLM" into simplified Chinese while keeping the HTML structure, you would use the following: ```html 评估应用程序是否从LLM中受益 ``` This maintains the structure and ensures the text is properly displayed in HTML, preserving any formatting needed for web content.

Sure, here is the translated text in simplified Chinese while keeping the HTML structure intact: ```html 当考虑一个法律硕士(LLM)是否能有效解决问题时,我们需要评估几个关键方面。以下是一个简短的指南,帮助确定一个申请是否适合LLM: ``` This HTML structure ensures that the translated text is properly formatted for use in a webpage or document.

To translate "Complexity of the Task" to simplified Chinese while keeping the HTML structure intact, you would use the following: ```html 任务的复杂性 ``` Here's how it breaks down: - `` indicates a span of text with the language attribute set to simplified Chinese. - `任务的复杂性` is the translation of "Complexity of the Task" into simplified Chinese characters. This HTML structure ensures that the text is correctly identified and displayed as simplified Chinese by browsers and other applications that support HTML markup.

Sure, here is the text translated into simplified Chinese while keeping the HTML structure: ```html

任务是否可以分解为理解和逻辑步骤,LLM 能够处理?

``` In this translation: - `

` denotes a paragraph in HTML, maintaining the structure of the text. - The simplified Chinese text is enclosed within the `

` tags, ensuring it is presented as a paragraph in the HTML document.

  • Sure, here's the translation in simplified Chinese while keeping the HTML structure intact: ```html 示例:写电子邮件涉及理解对话的背景、使用适当的语调以及逻辑地构建内容。 ``` In this HTML structure, the text is enclosed within `` tags to maintain the original structure.
  • Sure, here is the translated text in simplified Chinese, keeping the HTML structure intact: ```html 考虑因素:如果任务涉及自然语言理解、文本生成或总结,那么LLM可能非常合适。 ```

To translate "2. Context Sensitivity" into simplified Chinese while keeping the HTML structure intact, you would use the following: ```html 2. 上下文敏感性 ``` In this HTML snippet: - `...` is used to indicate that the text inside is in simplified Chinese. - "上下文敏感性" is the translation of "Context Sensitivity" into simplified Chinese characters.

在保留HTML结构的情况下,将以下英文文本翻译为简体中文: 任务是否需要理解特定的背景或细微差别?

  • Sure, here's the simplified Chinese translation of the text while keeping the HTML structure intact: ```html

    例子:写邮件时需要考虑收件人、邮件的目的和期望的语气。

    ``` In this HTML snippet, the original English text has been translated to simplified Chinese and wrapped in a `

    ` (paragraph) tag to maintain the structure.

  • Sure, here's the HTML structure with the translated text in simplified Chinese: ```html

    考虑因素:如果应用程序需要根据各种输入(如对话的语气或用户提供的特定背景)调整响应,LLM 可以动态适应这些细微差别。

    ``` In this HTML snippet: - `

    ` tags are used to enclose the translated text, indicating it as a paragraph in the HTML structure. - The Chinese text provided is the translation of "Consideration: If the application requires tailoring responses based on various inputs (like the tone of the conversation or specific context provided by the user), an LLM can dynamically adjust to these nuances."

To translate "Variability" into simplified Chinese while keeping the HTML structure, you can use the following: ```html 变异性 ``` This HTML snippet ensures the text "Variability" is displayed in simplified Chinese ("变异性") and also indicates the language for proper rendering and accessibility.

Certainly! Here's the text translated into simplified Chinese, keeping the HTML structure intact: ```html

任务是否受益于生成多种变化或创意输入?

```
  • Sure, here's the translation in simplified Chinese while keeping the HTML structure: ```html

    不同的情境和语气可能导致多种可接受的电子邮件格式。

    ```
  • Consideration: 如果任务受益于创造力或需要多个可行选项(例如不同的电子邮件措辞方式),语言模型生成多样化回应的能力将非常有价值。

Sure, here is the translation of "4. Efficiency Gains" into simplified Chinese while maintaining the HTML structure: ```html 4. 效率提升 ``` In this HTML snippet, `效率提升` represents "Efficiency Gains" in simplified Chinese.

Sure, here's the translation of "Will automating this task save significant time and effort?" into simplified Chinese, keeping the HTML structure intact: ```html 自动化这项任务能节省大量时间和精力吗? ```

  • Sure, here's the text translated to simplified Chinese while keeping the HTML structure: ```html

    人们花费大量时间起草和完善电子邮件。一个能加快这一过程的助手可以显著提高生产力。

    ```
  • Sure, here's the translation of the text into simplified Chinese while keeping the HTML structure intact: ```html

    考虑到:如果任务重复或耗时,使用LLM自动化可以释放出更多时间用于更为关键的活动。

    ``` In this HTML snippet, the text "Consideration: If the task is repetitive or time-consuming, automating it with an LLM can free up time for more critical activities." is translated into simplified Chinese.

应用程序:个性化电子邮件写作助手

Sure, here's the translated text in simplified Chinese while maintaining the HTML structure: ```html 使用上述指南,让我们看看为什么个性化电子邮件写作助手是LLM的绝佳应用程序: ``` This HTML snippet includes the translated text in simplified Chinese characters.

  • Sure, here's the translated text in simplified Chinese while keeping the HTML structure intact: ```html

    复杂性:电子邮件写作涉及多个步骤,LLM 可以很好地处理,如理解语境和生成连贯的文字。

    ```
  • 在上下文敏感性方面:助手可以根据用户的具体输入动态调整电子邮件的内容和语气。
  • Sure, here's the translation of the text into simplified Chinese, while keeping the HTML structure intact: ```html 变化性:它可以生成各种电子邮件格式和样式,让用户选择最符合他们需求的那一种。 ``` This HTML snippet translates the English text "Variability: It can generate various email formats and styles, allowing users to choose the one that best fits their needs." into simplified Chinese.
  • Sure, here's the text translated into simplified Chinese while keeping the HTML structure: ```html

    效率提升:自动起草电子邮件可以节省用户大量时间和精力,提升他们的生产力。

    ``` In this translation: - "Efficiency Gains" is translated to "效率提升". - "Automating email drafting" is translated to "自动起草电子邮件". - "can save users significant time and effort, enhancing their productivity" is translated to "可以节省用户大量时间和精力,提升他们的生产力".

Sure, here's the translated text in simplified Chinese: ```html

考虑到这些要点,很明显,个性化电子邮件写作助手是LLM学生的适用且有益的应用。

```

To translate "Requirements" into simplified Chinese without altering the HTML structure, you would use the following: ```html 需求 ``` This ensures the text "Requirements" is translated to "需求" in simplified Chinese, while maintaining the HTML structure intact.

Sure, here is the translation of "Now let’s think about the requirements for this application" in simplified Chinese, keeping HTML structure: ```html 现在让我们来考虑这个应用程序的要求。 ```

To translate the English text to simplified Chinese while keeping the HTML structure, you can use the following: ```html Imagine you need to write an email. Maybe you’re updating a colleague about a project. You’d start by typing out the main points you want to cover. This brings us to our first requirement: the application must accept user-provided text as the basis for generating an email. ``` In simplified Chinese, it would be: ```html 想象一下,你需要写封电子邮件。也许你要更新同事关于一个项目的情况。你会开始输入你想要涵盖的主要内容。这就带我们来到第一个要求:应用程序必须接受用户提供的文本作为生成电子邮件的基础。 ``` This HTML structure ensures that the text remains formatted and can be easily integrated into a web page or document.

```html

现在,假设你正在回复客户询问项目截止日期的电子邮件。为了确保上下文清晰,你应该在回复中包含客户的电子邮件。因此,应用程序应允许用户指定生成的电子邮件应该回复的文本。

``` This HTML structure contains the translated text in simplified Chinese.

有时候,你需要在电子邮件中提供额外的细节,比如提及一个任务延迟了三天。这意味着我们的应用必须让用户能够向他们的电子邮件添加具体的上下文,以确保生成的内容是相关和完整的。

Certainly! Here's the translated text in simplified Chinese, keeping the HTML structure intact: ```html

接下来,考虑邮件的长度。如果您只需要快速更新,短邮件就足够了。但是如果是详细的项目提案,您就需要写一封较长的邮件。因此,应用程序应允许用户指定邮件的大致长度。

``` This HTML snippet translates the provided English text into simplified Chinese, ensuring clarity and structure are maintained.

在保持HTML结构的情况下,将以下英文文本翻译为简体中文: 电子邮件的语气也非常关键。您可能希望给客户使用正式的语气,给同事使用随意的语气。这意味着我们的应用必须允许用户选择电子邮件的所需语气,比如正式、随意或轻松。

Here is the translated text in simplified Chinese while keeping the HTML structure: ```html 我们已经有足够的条件开始实际着手处理了,如果出现其他问题,我们会立即回应。 ``` This HTML structure maintains the integrity of the text while providing the translation in simplified Chinese.

应用概述

To translate "So our API will basically get:" into simplified Chinese while keeping the HTML structure intact, you can use the following: ```html 所以我们的 API 基本上会得到: ``` This HTML code snippet maintains the structure and indicates that the text inside the `` element is in simplified Chinese.

  • Sure, here's the translation in simplified Chinese: ```html
    用户输入:用户在电子邮件中想要传达的主要内容
    ```
  • Sure, here's the HTML structure with the translated text: ```html
    回复至:用户正在回复的电子邮件文本(如果有)
    ```
  • Sure, here's the translation of "Some additional context" into simplified Chinese while keeping the HTML structure intact: ```html 一些额外的背景 ``` In this HTML snippet, the text "一些额外的背景" represents the translation of "Some additional context" in simplified Chinese.
  • Sure, here's the translation in simplified Chinese while maintaining the HTML structure: ```html 长度:电子邮件的期望长度(以字符为单位) ```
  • To keep the HTML structure intact while translating "Tone: The tone of the email (e.g. formal, casual, relaxed)" into simplified Chinese, you can use the following HTML and Chinese text: ```html 语气:电子邮件的语气(例如正式、随意、轻松) ``` In simplified Chinese characters, it reads: ```html 语气:电子邮件的语气(例如正式、随意、轻松) ``` This HTML snippet ensures that the text remains properly formatted and displayed while presenting the translated content in Chinese.

Sure, here's the translation in simplified Chinese while keeping the HTML structure: ```html

生成的电子邮件输出。

```
client with a json to server, then a jsonbresponse back to the client

Certainly! Here is the translated text in simplified Chinese while keeping the HTML structure intact: ```html

我们还应该记录这些信息,以便最终评估性能并进行改进,甚至仅供用户查看以前生成的电子邮件。这意味着我们的应用程序应记录所有电子邮件生成的请求和响应,以供将来参考和分析(另一个要求)。

```

在HTML结构中保持不变,将以下英文文本翻译为简体中文: 另一个相关的要求是我们有用户(人们可以创建账户、登录等)。我们在讨论用户及其能做的事情,但我们从未明确定义过这一点。

Sure, here's how you can write "Step-by-Step Tutorial" in simplified Chinese while maintaining HTML structure: ```html 逐步教程 ``` In this HTML snippet: - `` is used to enclose the text. - `lang="zh-CN"` specifies the language as simplified Chinese. - "逐步教程" is the translation for "Step-by-Step Tutorial" in simplified Chinese characters.

基本设置

To translate "Let’s start by creating a folder, so open your terminal where you want the project to be and type:" into simplified Chinese while keeping the HTML structure, you can represent it as follows: ```html 让我们从创建一个文件夹开始,因此打开您希望项目位于的终端,并键入: ``` This HTML snippet maintains the structure while providing the translated text in simplified Chinese.

mkdir email-assistant-api

Sure, here's the text translated into simplified Chinese while preserving the HTML structure: ```html 并实际进入文件夹 ``` This HTML code displays "并实际进入文件夹" in simplified Chinese.

cd email-assistant-api

To translate the provided English text into simplified Chinese and maintain the HTML structure, you can use the following: ```html Now we need to initialize a poetry project, poetry is what we will use for dependency management. If you don’t yet have poetry installed, start by running: 现在我们需要初始化一个诗歌项目,诗歌是我们将用于依赖管理的工具。如果你还没有安装诗歌,请开始运行: ``` In this HTML snippet: - `...` encloses the original English text. - `...` encloses the translated Chinese text. This approach ensures that both languages are correctly marked up within the HTML structure, maintaining clarity and semantic correctness.

pip install poetry

然后,在email-assistant-api文件夹中输入

poetry init

Sure, here is the translated text in simplified Chinese, keeping the HTML structure intact: ```html 写下所需的内容(默认为空白)然后按 Enter ```

poetry fields

Sure, here is the text translated to simplified Chinese while maintaining the HTML structure: ```html

直到你看到类似如下内容时,为交互依赖项键入“no”:

``` This HTML snippet contains the translated text in simplified Chinese within a `

` (paragraph) element.

Here's the translated text in simplified Chinese, formatted in HTML structure: ```html

然后按下回车键确认生成。您会注意到您的文件夹中生成了一个 pyproject.toml 文件,这是 poetry 用来管理依赖项的文件。

```

Sure, here's the translation of "Let’s start by adding our dependencies" in simplified Chinese while keeping HTML structure: ```html 让我们首先添加我们的依赖项 ```

poetry add fastapi fastcrud sqlmodel openai aiosqlite greenlet python-jose bcrypt

Sure, here's the translated text in simplified Chinese while keeping the HTML structure intact: ```html 现在你应该注意到还有一个 poetry.lock 文件,这是 poetry 保存已安装包的实际版本的方式。 ``` This HTML structure preserves the text while allowing it to be displayed and formatted correctly within an HTML document.

Sure, here's the translation in simplified Chinese while keeping the HTML structure intact: ```html

接下来是什么?

```

To translate the text into simplified Chinese while keeping the HTML structure intact, you can use the following: ```html

在这篇文章中,我们介绍了项目,定义了问题,并设置了开发环境。在下一部分中,我们将深入探讨项目的结构、数据库建模和初始API端点。

``` This HTML code preserves the structure of the text while displaying the simplified Chinese translation.

Sure, here is the translation in simplified Chinese while maintaining HTML structure: ```html

您的下一步是第二部分:项目结构,数据库建模和API端点。

```

To translate "Connect with Me" into simplified Chinese while keeping the HTML structure, you can use the following: ```html 连接我 ``` This translates directly to "Connect with Me" in simplified Chinese characters.

Sure, here is the translated text: 如果您有任何问题,想讨论技术相关的话题,或者分享您的反馈,请随时在社交媒体上联系我:

  • To translate "GitHub: igorbenav" to simplified Chinese while keeping the HTML structure, you would use the following: ```html GitHub: igorbenav ``` This maintains the original structure and inserts the translated text in simplified Chinese within a `` element with the language attribute set to "zh-CN" for Chinese (simplified).
  • Sure, here's the translation in simplified Chinese, keeping the HTML structure: ```html X/Twitter: igorbenav ``` Translated to simplified Chinese: ```html X/Twitter:igorbenav ```
  • Sure, here's the translation of "LinkedIn: Igor" into simplified Chinese while keeping the HTML structure: ```html LinkedIn: Igor ``` In this HTML snippet: - `LinkedIn:` remains in English. - `Igor` is translated into simplified Chinese and wrapped in a `` tag with `lang="zh-CN"` attribute to specify the language.

2024-06-25 04:23:51 AI中文站翻译自原文