首页 >后端开发 >Python教程 >使用 Lyzr.ai 转换文本:分步指南

使用 Lyzr.ai 转换文本:分步指南

PHPz
PHPz原创
2024-08-07 08:04:121041浏览

Transform Your Text with Lyzr.ai: A Step-by-Step Guide

写作是我们日常生活的重要组成部分。无论是起草电子邮件、创建文档还是讲述故事,我们都力求清晰和准确。然而,使用拼写检查器纠正错误可能具有挑战性。

人工智能校对登场,这是一款旨在润色文本的出色工具。今天,我们将探索使用 AI 来改进写作、纠正语法、拼写、标点符号和格式的简单代码。

问题陈述

创建语法正确的文本至关重要,但通常很困难。手动校对非常耗时并且可能会漏掉错误。此代码使用Lyzr.ai检查和编辑文本,提高写作效率。

先决条件

开始之前,您应该了解 Python 编程并可以使用 API 密钥访问 OpenAI API。熟悉安装和导入 Python 库和 Lyzr.ai 框架也会有所帮助。

安装 Lyzr Automata 框架

pip install lyzr-automata

# For Google Colab or notebook
!pip install lyzr-automata

代码及说明

让我们逐步分解代码。

from lyzr_automata.ai_models.openai import OpenAIModel
from lyzr_automata import Agent, Task
from lyzr_automata.tasks.task_literals import InputType, OutputType
from lyzr_automata.pipelines.linear_sync_pipeline import LinearSyncPipeline
from lyzr_automata import Logger

API_KEY = input('Enter OpenAI API Key')
text = input('Enter the Text Here: ')

我们首先从 Lyzr.ai 库导入必要的工具,并提示用户输入 OpenAI API 密钥和文本进行校对。

open_ai_model_text = OpenAIModel(
    api_key=API_KEY,
    parameters={
        "model": "gpt-4-turbo-preview",
        "temperature": 0.5,
        "max_tokens": 1500,
    },
)

我们使用 API 密钥和参数设置 AI 模型,控制 AI 的行为和响应长度。

def ai_proofreader(text):
    ProofReader = Agent(
        prompt_persona="""You are an expert proofreader who can find grammatical errors, and you excel at checking for grammar, spelling, punctuation, and formatting errors.""",
        role="AI Proofreader",
    )

    rephrase_text = Task(
        name="Rephrasing Text",
        agent=ProofReader,
        output_type=OutputType.TEXT,
        input_type=InputType.TEXT,
        model=open_ai_model_text,
        instructions=f"Check the entire text: '{text}' and rephrase it according to grammar, spelling, punctuation, and formatting errors. [Important] Avoid introduction and conclusion in the response.",
        log_output=True,
        enhance_prompt=False,
        default_input=text
    )

    remarks = Task(
        name="Remarks",
        agent=ProofReader,
        output_type=OutputType.TEXT,
        input_type=InputType.TEXT,
        model=open_ai_model_text,
        instructions=f"Check the entire text: '{text}' and provide remarks in bullet points according to grammar, spelling, punctuation, and formatting errors. [Important] Avoid introduction and conclusion in the response.",
        log_output=True,
        enhance_prompt=False,
        default_input=text
    )

    logger = Logger()

    main_output = LinearSyncPipeline(
        logger=logger,
        name="AI ProofReader",
        completion_message="App Generated all things!",
        tasks=[
            rephrase_text,
            remarks,
        ],
    ).run()

    return main_output

我们定义了一个名为 ai_proofreader 的函数。在内部,我们创建一个名为 ProofReader 的代理,充当专家校对员。创建了两项任务:一项用于改写文本,另一项用于提供注释。这两项任务都使用 ProofReader 代理和 AI 模型。

记录器监视该过程。然后,我们建立一个顺序执行任务的管道,生成正确的文本和注释。

generated_output = ai_proofreader(text=text)
rephrased_text = generated_output[0]['task_output']
remarks = generated_output[1]['task_output']

我们使用用户的文本调用该函数,并获取改写的文本和注释作为输出。

示例输入

text = """ I Rajesh have 2+ years of experience in python developer, 
I know to create backend applications, 
I am seeking a new role for new learnings """

输出

""" 
My name is Rajesh, and I possess over two years of experience as a Python developer. 
I am skilled in creating backend applications and am currently seeking a new role to further my learning 

- The phrase "I Rajesh have 2+ years of experience in python developer" should be corrected to "I, Rajesh, have over two years of experience as a Python developer." This correction addresses a punctuation issue (adding commas around "Rajesh"), a numerical expression ("2+" to "over two"), and clarifies the role ("in python developer" to "as a Python developer").
- "python" should be capitalized to "Python" to properly denote the programming language.
- The phrase "I know to create backend applications" could be more fluidly expressed as "I know how to create backend applications" or "I am skilled in creating backend applications" for clarity and grammatical correctness.
- The phrase "I am seeking a new role for new learnings" could be improved for clarity and professionalism. A better alternative might be "I am seeking a new role to further my learning" or "I am seeking a new role to continue my professional development."
- The entire passage could benefit from better punctuation and formatting for clarity and flow. For instance, using semicolons or periods to separate independent clauses can improve readability: "My name is Rajesh, and I possess over two years of experience as a Python developer; I am skilled in creating backend applications and am currently seeking a new role to further my learning."
- Consistency in tense and style would improve the professional tone of the passage.
"""

关于 Lyzr.ai

Lyzr.ai 提供低代码代理开发套件,用于快速创建 GenAI 应用程序。借助这个简单的代理框架,您可以构建安全可靠的生成式 AI 应用程序,用于各种用途,包括校对和写作。

参考文献

如需了解更多信息,请访问 Lyzr 的网站、预订演示或加入 Discord 和 Slack 上的社区频道。

  • Lyzr 网站
  • 预订演示
  • Lyzr 社区频道:Discord、Slack

人工智能校对器: GitHub

以上是使用 Lyzr.ai 转换文本:分步指南的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn