跳转到主要内容

用于在Python运行时请求OpenAI GPT生成对象的库。

项目描述

达芬奇函数

用于在Python运行时请求OpenAI GPT生成对象的库。

此库旨在记录对Python程序有用的提示。如果您开发了某些内容,请发起拉取请求!

入门

在开始使用此库之前,请设置OpenAI组织ID和API密钥。然后调用库中的函数。

import openai
import davinci_functions

openai.organization = "YOUR_ORG_ID"
openai.api_key = "YOUR_API_KEY"

prompt = """\
Output the list of 10 random city names in the United States.
"""

for val in davinci_functions.list(prompt):
    print(val)

此脚本将打印类似以下内容:

New York
Los Angeles
Chicago
Houston
Phoenix
Philadelphia
San Antonio
San Diego
Dallas
San Jose

函数

davinci_functions.list

返回某个列表。

>>> davinci_functions.list("say hello.")
['Hello']
>>> davinci_functions.list("say hello world.")
['Hello', 'world']
>>> davinci_functions.list("Output first 5 prime numbers.")
[2, 3, 5, 7, 11]
>>> davinci_functions.list("5 random countries")
['Japan', 'Australia', 'Brazil', 'India', 'China']

解决某些任务(例如,命名实体识别)

>>> prompt="""
... Extract all named entities in the following paragraph:
... 
... Google is founded by Larry Page and Sergey Brin in 1998.
... The headquarter is located in Mountain View, Carifornia, United States.
... """
>>> davinci_functions.list(prompt)
['Google', 'Larry Page', 'Sergey Brin', 'Mountain View', 'Carifornia', 'United States']

其他语言(日语)

>>> davinci_functions.list("日本語の単語を5個")
['日本語', '単語', '文字', '言葉', '意味']
>>> davinci_functions.list("1から10までの数字のリスト。ただし3で割り切れるときはFizzにしてください。")
[1, 2, 'Fizz', 4, 5, 'Fizz', 7, 8, 'Fizz', 10]
>>> davinci_functions.list("「明日は明日の風が吹く」の形態素の一覧")
['明日', 'は', '明日', 'の', '風', 'が', '吹く']

davinci_functions.judge

返回某个的真相。

>>> davinci_functions.judge("The sum of 2 and 3 is 5.")
True
>>> davinci_functions.judge("The sum of 2 and 3 is 6.")
False
>>> davinci_functions.judge("San Francisco is the capital of the United States.")
False
>>> davinci_functions.judge("New York is the capital of the United States.")
True  # Wrong answer! This kind of mistakes happens very often: please take care.
>>> davinci_functions.judge("Washington D.C. is the capital of the United States. How about New York?")
False

davinci_functions.function

根据提示生成Python函数。

此函数不安全。请勿在实际产品中使用此函数。

>>> f = davinci_functions.function("Multiply the argument x by 2.")
>>> f(3)
6
>>> f = davinci_functions.function("Arithmetic mean of all elements in the list x.")
>>> f([1, 2, 3])
2.0
>>> f = davinci_functions.function("""\
... Given a list of unique integers x, return two positions so that the sum of the
... numbers on that positions is equal to the argument y.
... The function must be efficient as well as possible.
... """)
>>> f([1, 4, 9, 16, 25], 25)
(3, 2)

davinci_functions.explain

描述给定函数的行为。

>>> def f(x):
...     return x * 3
... 
>>> davinci_functions.explain(f)
'This function takes a variable x and multiplies it by 3, then returns the result.'
>>> def f(a, b, c):
...     return (-b + math.sqrt(b**2 - 4.0 * a * c)) / (2.0 * a)
... 
>>> davinci_functions.explain(f)
'This function implements the Quadratic Formula to calculate the solution of a ...
 quadratic equation. The equation is of the form ax^2 + bx + c = 0. The function ...
 takes three parameters a, b, and c, which are the coefficients of the equation. It ...
 then calculates the solution using the formula (-b + sqrt(b^2 - 4ac)) / (2a) and ...
 returns the result.'

注意事项

目前,此库不考虑提示注入和GPT返回表达式的有效性。请勿在需要关注一致性和安全性的实际产品中使用此库。

项目详情


下载文件

下载适用于您平台文件。如果您不确定选择哪一个,请了解更多关于 安装包 的信息。

源分发

davinci_functions-0.1.4.tar.gz (6.9 kB 查看散列值)

上传时间

构建分发

davinci_functions-0.1.4-py3-none-any.whl (9.0 kB 查看散列值)

上传时间 Python 3