GPT作为最小自然语言处理器,又称MNLPs

102 阅读3分钟

本文旨在利用GPT-3和GPT-3.5模型实现一些自然语言处理的想法,其功能是相当深入和准确的文本分析,产生小的构建块,用于更复杂的系统。

image.png

Bool语句处理器:这个处理器的想法是使用GPT-3的配置和一个合适的语句和正确的问题来提示模型基于一个预期值完成。这就是TL;TR:https://github.com/DevArKaDiA/MNLPs。

from pydash import getfrom typing import List, Optional, Union, castimport openaifrom pydantic import BaseModelfrom openai.openai_object import OpenAIObjectopenai.api_key = "your-api-key"class CompletionParams(BaseModel):    model: str    prompt: Optional[Union[str, List[Union[str, List[str]]]]] = None    suffix: Optional[str] = None    max_tokens: Optional[int] = 16    temperature: Optional[float] = 1    top_p: Optional[float] = 1    n: Optional[int] = 1    stream: Optional[bool] = False    logprobs: Optional[int] = None    echo: Optional[bool] = False    stop: Optional[Union[str, List[str]]] = None    presence_penalty: Optional[float] = 0    frequency_penalty: Optional[float] = 0    best_of: Optional[int] = 1    logit_bias: Optional[dict] = None    user: Optional[str] = Nonedef generate_completition(input: CompletionParams) -> OpenAIObject:    return openai.Completion.create(**input.dict(exclude_none=True))def bool_mnlp(statement: str, paylaod: str):    """    This function uses the OpenAI API to generate a natural language response to a given    statement and text, and returns a boolean value based on the response. The response    is generated using the text-davinci-003 model with a temperature of 0 and max_tokens of 2.    Parameters:    - statement: a string containing a statement to be evaluated by the model.    - paylaod: a string containing text to be used as context for the evaluation.    Returns:    - A boolean value, True if the model responds affirmatively (SI), False if it responds    negatively (NO).    Raises:    - TypeError: if the completion text is not found in the OpenAI response or has an    unexpected value.    """    rule = (        "'SI' IF AFFIRMATIVE, 'NO' IF NEGATIVE STATEMENT OF THE TEXT, OTHERWISE 'NO'"    )    statement = f"STATEMENT:{statement}"    text = f"TEXT:{paylaod}"    prompt = f"{rule}\n{statement}\n{text}\nRESPONSE:"    completition = generate_completition(        input=CompletionParams(            model="text-davinci-003",            temperature=0,            prompt=prompt,            max_tokens=2,        )    )    completition_text = get(completition, "choices.[0].text", default=None)    completition_text = cast(Optional[str], completition_text)    if completition_text is None:        raise TypeError("Text not found on completion")    completition_text = completition_text.upper()    if completition_text.find("NO") != -1:        return False    elif completition_text.find("SI") != -1:        return True    else:        raise TypeError(f"Bad completion text value {completition_text}")

现在我们来分解一下这段代码。首先,我们使用了3个库,主要是帮助我们做功用,我们还使用了OpenAI库作为他们的HTTP API的封装器,主要是为了让我们的请求更容易。

image.png

from pydash import getfrom typing import List, Optional, Union, castimport openaifrom pydantic import BaseModelfrom openai.openai_object import OpenAIObject

pydash:我们用它来访问OpenAIObject,它是一个具有简单符号的字典的子类。

Pydantic:我们用它来访问比赛的配置对象,因为参数是以一种不是很明确的方式接收的。

测试:

  • 简单的案例:
functions.bool_mnlp(      statement="habla de magia?", paylaod="la mecanica es una habilidad que todo hombre deberia manejar"  )

image.png

  • 混合Lenguajes
print(    functions.bool_mnlp(        statement="habla de magia?",        paylaod="the magic is a super interesting topic",    ))

image.png

通过这两个例子,我们可以看到,该函数的反应相当准确。这是由温度等参数造成的,除此之外。我正在研究一个更正式的实现和使用案例,如聊天机器人和ChatGPT,但我邀请你梦想一下系统层面上的可能性。

例如,网络刮削,我们获得所有的文本,并且,给定一个JSON模式,我们获得解析的字典或数据类或Pydantic模型,只需一个函数,该函数由这个GPT驱动。我想对这些函数做一个性能和成本审查,因为如果你对OpenAI的定价有更多的了解,它在很大程度上取决于传递给模型的令牌(提示)。这是在使用这个GPT的情况下,但我们也可以审查使用BLOOM的实现,这将是一个我们有可能在本地运行的模型,我相信它将有类似的性能,同时消除了调用API的成本。