尽管前赴后继有各种厂商声称在某些基准测试上超越了GPT 4,OpenAI旗下的ChatGPT仍然是用户量最高且公认的最优质的人工智能产品。OpenAI在GPT 2之后就不再开源其大模型,这也正是马斯克吐槽其应改名为ClosedAI。大佬们的口水战与我等无关,对于开发者来说,要在自己的系统里集成优秀的 GPT,就需要通过调用其 API。
本文的相关代码请见 GitHub
如何获取API Key?
调用 OpenAI的 API 无需订阅ChatGPT Plus。我们知道OpenAI在 APP 端和网页端都提供有其于 GPT 3.5的免费对话服务,而ChatGPT Plus 是为希望使用GPT 4、GPT Store及多模态等功能的用户所提供的付费订阅服务。我们获取 API Key与ChatGPT Plus并无直接关联,它主要要求绑定信用卡以供按使用量扣款。绑定信用卡可访问账单信息页面。成功绑定信用卡后进入API keys页面点击Create new secret key创建一个以sk-
开头API Key。
如何使用API Key?
API Key应妥善保管,最佳实践是不在代码中明文添加API Key。常见的做法是添加到环境变量中,在 Linux或Mac命令行中的设置方法是:
1 2 3 4 |
# 在当前会话中设置OPENAI_API_KEY环境变量 export OPENAI_API_KEY=sk-(...) # 检测环境变量是否已设置 echo $OPENAI_API_KEY |
在Windows下命令稍有不同:
1 2 3 4 |
# 在当前会话中设置OPENAI_API_KEY环境变量 set OPENAI_API_KEY=sk-(...) # 检测环境变量是否已设置 echo %OPENAI_API_KEY% |
以上是基于 Shell会话的设置方法,我们还可以将环境变量设置到全局,方便在所有程序中调用,Linux和 Mac 中可在.bashrc
、.zshrc
等文件中添加相应命令。Windows下则是在系统属性(sysdm.cpl
)的高级标签页设置全局环境变量。
有关API Key的最佳实践及设置方法也可参阅 OpenAI 的官方文档Best Practices for API Key Safety。
开发上调用OpenAI的API通常会使用Python,当然其社区也提供了其它主流语言的开发包,且其调用本质上就是HTTP请求,所以并不局限于任何编程语言。首先应安装Python官方包。
1 |
pip install openai |
从环境变量中读取并设置API Key的方法也很简单:
1 2 3 4 |
import os import openai openai.api_key = os.environ["OPENAI_API_KEY"] |
但如果按照官方的环境变量名称进行设置实际上并不需要编写这段代码,官方支持的环境变量名称有:
- OPENAI_API_KEY:必填项,API Key
- OPENAI_ORG_ID:非必填,组织ID,大部分开发者可忽略,猜测是用于向部分企业开放试用或高级功能,官方 Python 库中显示的都是
organization': '*'
。 - OPENAI_BASE_URL:非必填,默认值
https://api.openai.com/v1
,在使用代理或非官方渠道购买服务时使用。
同时在项目中我们一般通过配置文件的方式来设置环境变量,创建一个.env
文件:
1 2 |
OPENAI_API_KEY=sk-xxxx OPENAI_BASE_URL=https://api.openai.com/v1 |
安装dotenv包(pip install python-dotenv
)进行加载即可:
1 2 3 4 5 |
from openai import OpenAI from dotenv import load_dotenv, find_dotenv _ = load_dotenv(find_dotenv()) client = OpenAI() |
开发一个Hello World 程序
OpenAI的SDK在1.0.0前后并不兼容,对于老版本的 SDK,调用方法是:
1 2 3 4 5 6 7 8 |
import openai # Call the openai ChatCompletion endpoint response = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=[{"role": "user", "content": "Hello World!"}], ) # Extract the response print(response["choices"][0]["message"]["content"]) |
此时会看到输出内容:
1 |
Hello there! How may I assist you today? |
而在1.0.0之后版本以上代码会报错:
1 2 3 4 5 6 7 |
APIRemovedInV1: You tried to access openai.ChatCompletion, but this is no longer supported in openai>=1.0.0 - see the README at https://github.com/openai/openai-python for the API. You can run `openai migrate` to automatically upgrade your codebase to use the 1.0.0 interface. Alternatively, you can pin your installation to the old version, e.g. `pip install openai==0.28` |
报错信息也很明确了,就是移除了这一 API,解决方法一是降级安装包,如执行pip install openai==0.28
,或是使用官方的工具也即openai migrate
命令自动升级代码。考虑到
新版的执行方法:
1 2 3 4 5 6 7 8 9 10 11 |
client = OpenAI() completion = client.chat.completions.create( model="gpt-3.5-turbo", messages=[ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "What are some famous astronomical observatories?"} ] ) print(completion) |
此时会输出:
1 |
ChatCompletion(id='chatcmpl-97HHgGgfbu149QfJarPBBrgfcA2A5', choices=[Choice(finish_reason='stop', index=0, logprobs=None, message=ChatCompletionMessage(content='There are several famous astronomical observatories around the world. Some of the most well-known ones include:\n\n1. The Hubble Space Telescope: While not an observatory in the traditional sense, the Hubble Space Telescope is one of the most iconic and influential astronomical instruments. It is located in low Earth orbit and has provided stunning images and data about the universe since its launch in 1990.\n\n2. The Mauna Kea Observatory: Located on the summit of Mauna Kea in Hawaii, this multi-institutional facility houses several world-class telescopes, including the W. M. Keck Observatory and the Subaru Telescope. It is known for its ideal observing conditions and its contribution to groundbreaking discoveries.\n\n3. The Arecibo Observatory: Situated in Puerto Rico, the Arecibo Observatory features the largest single-dish radio telescope in the world. It has been instrumental in the study of pulsars, gravitational waves, and the search for extraterrestrial intelligence.\n\n4. The Very Large Array (VLA): Located in New Mexico, USA, the VLA is a collection of 27 radio antennas that can be arranged in different configurations to conduct high-resolution imaging of celestial objects. It has provided invaluable insights into the structure and dynamics of the universe.\n\n5. The Chandra X-ray Observatory: Launched by NASA in 1999, the Chandra X-ray Observatory is designed to detect X-ray emissions from high-energy sources in space. It has helped scientists study black holes, supernovae, and other extreme cosmic phenomena.\n\n6. The Atacama Large Millimeter Array (ALMA): Situated in the Atacama Desert in Chile, ALMA is a collection of 66 high-precision antennas that observe the universe in millimeter and submillimeter wavelengths. It has revolutionized our understanding of star formation, galaxy evolution, and the origins of life.\n\nThese are just a few examples of the many famous observatories across the globe that contribute to our understanding of the cosmos.', role='assistant', function_call=None, tool_calls=None))], created=1711521764, model='gpt-3.5-turbo-0613', object='chat.completion', system_fingerprint=None, usage=CompletionUsage(completion_tokens=403, prompt_tokens=25, total_tokens=428)) |
可以在这里看到官方对消息封装了一个ChatCompletionMessage
对象,所以徜若按之前访问字典的方式执行print(completion["choices"][0]["message"]["content"])
,会报如下错误:
1 |
TypeError: 'ChatCompletion' object is not subscriptable |
输出内容只需要按操作对象的方式执行print(completion.choices[0].message.content)
即可。
除了编写代码,OpenAI也提供了命令行调用的方式,如:
1 2 |
openai api chat.completions.create -m gpt-3.5-turbo \ -g user "Hello world" |
生成结果中的主要字段有:
- choices:这是包含模型实际响应的数组,通常只有一个元素,但可以通过在创建会话时传入参数
n
进行修改。这个元素的主要字段有:- finish_reason:回复结束的原因,上例中原因为
stop
,表示从模型得到了完整的回复。生成输出产生错误时,会在这一字段中有相应体现。 - index:choices 数组中当前对象的索引。
- message:包含
role
及内容content
或function_call
(未来会被tool_calls
替代)。此外的role
均为assistant
。
- finish_reason:回复结束的原因,上例中原因为
- created:按时间戳显示的生成时间。
- id:OpenAI 内部使用的标识符。
- model:所使用的模型,与在输入中所设置的一致。
- object:不论是GPT-4还是GPT3.5模型,一律为chat.completion,因为我们使用的 endpoint 就是对话补全。
- usage:本次查询所使用的 token 数,也即为计价依据。prompt_tokens表示输入中使用的 token 数,completion_tokens为输出的 token数,那么总 token 数也就很容易计算了,
total_tokens
=prompt_tokens
+completion_tokens
Embedding和Token
embedding和token 的概念和具体作用计划单独放在一节中进行讲解,以下简单介绍下如何通过代码来生成相应的结果,首先 OpenAI API 中提供了创建 embedding 的方法:
1 2 3 4 5 6 |
response = client.embeddings.create( model="text-embedding-ada-002", input="The food was delicious and the waiter..." ) print(response) |
输出结果为:
1 |
CreateEmbeddingResponse(data=[Embedding(embedding=[0.0022786001209169626, -0.0092921182513237, 0.015702664852142334, -0.007711696904152632, -0.004709530156105757, 0.014915627427399158, -0.00985700823366642, -0.03820939362049103, -0.006931006442755461, -0.028587227687239647, 0.025197889655828476, 0.01813994161784649, -0.003643221454694867, -0.025540631264448166, 0.0005629062652587891, -0.0164389256387949, 0.02833334542810917, 0.005388667341321707, 0.00969198439270258, -0.01641353778541088, -0.015398005023598671, 0.00433187885209918, 0.006956394761800766, -0.0072674015536904335, -0.003890757216140628, 0.01857154257595539, 0.008708187378942966, -0.02267175354063511, 0.011475512757897377, 0.023877698928117752, 0.01562649942934513, -0.003535320982336998, -0.0348835289478302, -0.004214458167552948, -0.026149950921535492, -0.021491197869181633, -0.0057155415415763855, 0.011761130765080452, 0.00844795722514391, 0.004112904891371727, 0.019168168306350708, -0.014407861046493053, 0.008936682716012001, 0.006413719616830349, -0.045698944479227066, 0.01788605935871601, -0.0055790795013308525, -0.0007513350574299693, -0.022113211452960968, -0.00393518665805459, 0.021008819341659546, -0.017594093456864357, -0.011678619310259819, -0.022620977833867073, 0.01632467843592167, 0.017149798572063446, -0.008403528481721878, 0.0015788350719958544, 0.025058254599571228, -0.024994783103466034, 0.007825944572687149, 0.005826615262776613, -0.02218937687575817, 0.0029339357279241085, -0.006172530818730593, -0.025553327053785324, -0.008067132905125618, 0.0010377469006925821, 0.00021738733630627394, 0.004649232607334852, 0.02074224315583706, 0.013443105854094028, 0.0046270182356238365, -0.01598193682730198, 0.016591254621744156, -0.008968417532742023, -0.0075974492356181145, 0.01365890633314848, -0.0070071713998913765, 0.00529346102848649, 0.009876049123704433, -0.045952826738357544, 0.0030370757449418306, 0.02400464005768299, 0.023039884865283966, 0.007026212755590677, -0.02357303909957409, 0.009914131835103035, -0.006569223012775183, -0.03333484008908272, -0.002678465796634555, 0.019904429093003273, 0.0017057765508070588, 0.001082969713024795, -0.022684447467327118, 0.005020536947995424, 0.015423393808305264, 0.03148149326443672, -0.005423576105386019, -0.015943853184580803, -0.005134784150868654, 0.019853651523590088, -0.009368283674120903, -0.0062010926194489, -0.031202223151922226, -0.009438101202249527, -0.015271063894033432, -0.02879033423960209, 0.021034209057688713, -0.018279576674103737, -0.0029371092095971107, 0.012713192030787468, 0.005099875386804342, -0.049557965248823166, -0.043007783591747284, -0.00041692351805977523, 0.021580057218670845, -0.01688322052359581, 0.006562876049429178, -0.04148448631167412, 0.0009155656443908811, 0.033487170934677124, 0.012928992509841919, -0.010491715744137764, -0.0005823441897518933, 0.018381129950284958, 0.00011305728548904881, -0.013125752098858356, 0.01194519642740488, 0.005785359535366297, 0.007057948037981987, 0.00861298106610775, 0.018431907519698143, -0.0008941442938521504, -0.019168168306350708, 0.021960880607366562, -0.029755089432001114, -0.004296970088034868, 0.0014875958440825343, -0.005163345951586962, 0.014979097992181778, 0.021389644593000412, -0.01880003698170185, 0.011062952689826488, -0.007457813713699579, 0.0226590596139431, 0.0191554743796587, 0.014966404065489769, -0.0036654360592365265, 0.006277257576584816, 0.025692962110042572, -0.027774803340435028, 0.0367368720471859, -0.004398523364216089, 0.01387470681220293, 0.007984621450304985, -0.0056362031027674675, 0.010288609191775322, -0.005813921336084604, -0.008682799525558949, 0.010612309910356998, -2.8388287319103256e-05, 0.0321669802069664, -0.02389039285480976, -0.0026356230955570936, 0.03036440908908844, 0.029602760449051857, 0.016616644337773323, 0.0021738733630627394, 0.0010607549920678139, -0.011627842672169209, 0.018508072942495346, -0.007610143627971411, 0.007432425394654274, -0.0011266059009358287, 0.004493729677051306, 0.009945867583155632, -0.007533978670835495, -0.006210613530129194, -0.0057377563789486885, -0.028282567858695984, 0.004312837962061167, 0.029323488473892212, 0.024766288697719574, -0.01563919335603714, -0.018190719187259674, 0.00750859035179019, 0.008689146488904953, -0.011716701090335846, -0.006366116926074028, 0.001986634684726596, 0.036889202892780304, -0.0023420709185302258, -0.020805712789297104, -0.6889369487762451, -0.01959976926445961, 0.0035258005373179913, 0.0067342473194003105, 0.02833334542810917, 0.02345879189670086, 0.007184889633208513, 0.010631351731717587, 0.009609472006559372, -0.009450795128941536, -0.02131347917020321, 6.0396389017114416e-05, 0.019777487963438034, 0.0022786001209169626, -0.009615819901227951, -0.0020627996418625116, -0.0004129566077608615, 0.006619999650865793, -0.04135754331946373, 0.011189893819391727, -0.013392329216003418, 0.027698637917637825, -0.011418389156460762, 0.00022532118600793183, 0.01546147558838129, -0.0005335510359145701, 0.014471332542598248, -0.00854951050132513, -0.018901590257883072, -0.017721034586429596, -0.009438101202249527, 0.024613959714770317, 0.012954381294548512, -0.008974764496088028, 0.034959692507982254, -0.005109395831823349, -0.024207746610045433, 0.024042721837759018, -0.003773336298763752, 0.03104989416897297, -0.01501718070358038, -0.023268379271030426, 0.030161302536725998, 0.0068929241970181465, 0.010117238387465477, 0.011424736119806767, 0.030339021235704422, 0.017644869163632393, 0.0021119893062859774, -0.0067342473194003105, 0.009711025282740593, 0.016629338264465332, 0.0036463949363678694, 0.027952520176768303, 0.004068475216627121, 0.0030941993463784456, 0.029856642708182335, -0.01688322052359581, 0.002145311562344432, 0.02323029562830925, 0.011767477728426456, 0.014319002628326416, -0.019790181890130043, -0.029602760449051857, -0.001570901251398027, 0.018419213593006134, -0.002787953009828925, 0.019345887005329132, 0.027571696788072586, -0.008454304188489914, 0.012389491312205791, 0.002821275033056736, -0.00529346102848649, 0.006458149291574955, 0.009317507036030293, 0.022608283907175064, 0.02357303909957409, 0.0010218791430816054, 0.018939673900604248, -0.002981538651511073, -0.006854841485619545, -0.010129932314157486, -0.007902109064161777, -0.007286442909389734, 0.020475665107369423, 0.010885234922170639, -0.029882032424211502, -0.003855848452076316, 0.002765738172456622, -0.01892697997391224, 0.03000897355377674, 0.024563182145357132, -0.004433432128280401, 0.02650538831949234, 0.017581399530172348, 0.04120521619915962, -0.02015831135213375, 0.014623662456870079, 0.021554669365286827, -0.029983585700392723, -0.0014764884253963828, 0.0034686767030507326, 0.03422342985868454, 0.036076776683330536, 0.028028685599565506, 0.01845729537308216, 0.0034020324237644672, 0.009336547926068306, 0.02074224315583706, -0.012535474263131618, 0.017022857442498207, 0.011532636359333992, -0.007737085223197937, 0.01331616472452879, -0.0074260784313082695, -0.022455953061580658, 0.0058964332565665245, 0.0257310438901186, -0.019079308956861496, -0.01902853325009346, -0.013328858651220798, -0.002640383318066597, 0.022519424557685852, -0.007927496917545795, -0.0007882274221628904, 0.028130238875746727, 0.014027036726474762, -0.019980594515800476, -0.021110372617840767, -0.0029260017909109592, 0.013455799780786037, 0.01609618403017521, 0.011913460679352283, -0.016794361174106598, 0.004830124322324991, -0.013036892749369144, 0.025756431743502617, -0.03036440908908844, 0.017594093456864357, -0.02241787128150463, -0.018838120624423027, 0.0019533124286681414, -0.0061122337356209755, 0.0021373776253312826, -0.0014566538156941533, -0.0111518120393157, -0.0052839405834674835, -0.011418389156460762, -0.02163083292543888, 0.005169692914932966, 0.0055790795013308525, -0.00022809802612755448, -0.01275127474218607, 0.0034242472611367702, 0.024664735421538353, -0.001767660491168499, 0.0012813159264624119, -0.005414055660367012, -0.024880535900592804, -0.030618293210864067, -0.01607079617679119, 0.016680113971233368, -0.013189222663640976, -0.008739923126995564, 0.009526960551738739, -0.0073689548298716545, 0.0070198653265833855, -0.005696500185877085, -0.012078484520316124, -0.02377614565193653, 0.02879033423960209, -0.015905771404504776, -0.003325867699459195, 0.005236337427049875, -0.01678166724741459, 0.0014153978554531932, -0.02185932733118534, -0.019561687484383583, -0.006331207696348429, 0.016680113971233368, 0.008771657943725586, 0.004179549403488636, -0.013811236247420311, -0.01285282801836729, 0.00946348998695612, 0.005798053462058306, 0.0014153978554531932, 0.025337524712085724, -0.01777181215584278, 0.015359923243522644, 0.002007262548431754, -0.011094688437879086, 0.007400690112262964, 0.0015756614739075303, 0.015956547111272812, 0.013455799780786037, -0.0026499039959162474, 0.0021215099841356277, 0.014141283929347992, -0.0023388974368572235, -0.004271581768989563, -0.006524793803691864, 0.0030703977681696415, -0.004715877119451761, 0.013239999301731586, -0.017492540180683136, -0.011113729327917099, -0.029704313725233078, 0.0036876508966088295, 0.0074705081060528755, 0.016108877956867218, -0.01382393017411232, -0.017251351848244667, -0.0037892041727900505, 0.021440420299768448, 0.017060939222574234, 0.019295109435915947, 0.00688022980466485, -0.013950872235000134, -0.009247689507901669, -0.0023420709185302258, -0.020932655781507492, -0.00521094910800457, -0.0014844222459942102, 0.006426414009183645, 0.0030497699044644833, 0.0029323489870876074, -0.007330872118473053, 0.0012749688467010856, -0.0021595924627035856, -0.0019136433256790042, 0.01859693042933941, 0.0007255500531755388, 0.015169510617852211, 0.0070071713998913765, 0.001370968297123909, 0.016388149932026863, -0.008701840415596962, 0.016730891540646553, -0.00047682406147941947, -0.007679961621761322, 0.016730891540646553, -0.0008163925958797336, -0.02810485102236271, 0.010916969738900661, 9.85284277703613e-05, -0.006124928127974272, -0.011259712278842926, -0.027444753795862198, 0.0088986000046134, 0.012890910729765892, 0.016845138743519783, -0.017619481310248375, 0.014737909659743309, -0.004008178133517504, 0.0080417450517416, -0.0018993623089045286, -0.01880003698170185, 0.03419804200530052, 0.020983431488275528, 0.016515091061592102, 0.02220207080245018, 0.015423393808305264, -0.0009647554834373295, 0.004408043809235096, 0.0004982454120181501, -0.007718043867498636, -0.026200728490948677, 0.009837967343628407, -0.0067025115713477135, 0.014763297513127327, -0.025921456515789032, -0.002384913619607687, -0.006613652687519789, 0.006696164608001709, -0.0012749688467010856, -0.018660401925444603, 0.002205608645454049, -0.017847975715994835, 0.004890421871095896, 0.0031497362069785595, -0.021795857697725296, 0.029856642708182335, 0.011837296187877655, -0.0027117880526930094, -0.011031216941773891, -0.017365599051117897, 0.0073245251551270485, -0.0124021852388978, 0.0033322146628051996, 0.007172195240855217, 0.014395167119801044, 0.0011821428779512644, -0.009641207754611969, 0.014090507291257381, 0.0006136828451417387, 0.04613054543733597, -0.020754937082529068, 0.031659211963415146, -0.005156998988240957, 0.006569223012775183, -0.025312136858701706, -0.02820640429854393, -0.023496873676776886, 0.026226116344332695, -0.013100364245474339, -0.022290930151939392, -0.000551005476154387, 0.023357238620519638, -0.007749779149889946, 0.006518446374684572, -0.006039242260158062, -0.012535474263131618, -0.00216593942604959, 0.023979252204298973, -0.0033893382642418146, -0.02945042960345745, 0.0019564861431717873, 0.007692655548453331, -0.0008473345660604537, -0.030389796942472458, -0.017060939222574234, -0.008765310980379581, -0.013519271276891232, 0.07804363965988159, -0.0048967688344419, 0.01523298118263483, 0.008257545530796051, 0.0367368720471859, 0.0005073693464510143, -0.03884410113096237, -0.004649232607334852, -0.005426749587059021, -0.012141955085098743, -0.006785023957490921, 0.009704678319394588, 0.02493131347000599, 0.007140459958463907, 0.026327669620513916, -0.0043001435697078705, -0.005905953701585531, -0.024550488218665123, 0.019333191215991974, -0.023826921358704567, -0.00821946281939745, 0.016603950411081314, 0.01598193682730198, 0.027140095829963684, -0.01524567510932684, 0.004373135045170784, 0.025451773777604103, 0.02617533877491951, -0.001132952980697155, -0.023395320400595665, 0.0015994630521163344, -0.0012456135591492057, -0.004988801199942827, -0.002383326878771186, -0.015537641011178493, 0.004801562521606684, 0.0004486589168664068, -0.012275244109332561, 0.004893595352768898, -0.0062709106132388115, 0.04054511711001396, -0.008752617053687572, 0.005131610669195652, -0.016921304166316986, 0.004649232607334852, -0.0015597938327118754, -0.027724025771021843, 0.05095432326197624, -0.030415186658501625, -0.0028799856081604958, 0.015398005023598671, 0.02706393040716648, 0.00230716192163527, -0.010390162467956543, -0.004360441118478775, 0.011177199892699718, -0.0066326940432190895, -0.024283910170197487, -0.014027036726474762, 0.016261206939816475, -0.014699826948344707, -0.03770162910223007, -0.0055346498265862465, -0.0007910042768344283, -0.02593415044248104, -0.020450277253985405, -0.012611638754606247, -0.012421227060258389, -0.0028577707707881927, -0.002941869432106614, -0.012446614913642406, 0.0037511216942220926, -0.012808398343622684, -0.007026212755590677, 0.017365599051117897, -0.0026023010723292828, -0.003436941420659423, -0.009774496778845787, -0.000974276103079319, 0.009901437908411026, 0.0007866406813263893, -0.008213115856051445, 0.0013923896476626396, 0.003621006617322564, -0.029196547344326973, 0.022697141394019127, 0.02660694159567356, 0.011094688437879086, 0.0054108817130327225, 0.0044239116832613945, -0.002184980781748891, -0.002527722856029868, 0.005141131114214659, -0.015588417649269104, -0.0054553113877773285, -0.02197357639670372, -0.016261206939816475, 0.025172501802444458, -0.008409875445067883, -0.0032497027423232794, -0.008136950433254242, 0.0019104697275906801, -0.009285771287977695, 0.0006160630146041512, 0.043667878955602646, -0.008511428721249104, -0.011354918591678143, -0.02504556067287922, 0.004252540413290262, -0.011126423254609108, 0.020475665107369423, 0.014877544716000557, -0.019015837460756302, 0.0075403256341814995, -0.012078484520316124, 0.03049135021865368, 0.012154649943113327, 0.03239547461271286, 0.0035924448166042566, -0.009082664735615253, -0.007413384038954973, -0.006420067045837641, 0.02504556067287922, -0.012224467471241951, 0.009507918730378151, -0.0010147386929020286, 0.012446614913642406, -0.013239999301731586, -0.0014479266246780753, -0.01273858081549406, -0.0036337007768452168, 0.007127766031771898, -0.010409204289317131, 0.010072808712720871, -0.004328705370426178, -0.002559458138421178, -0.004112904891371727, 0.010866193100810051, -0.025667574256658554, -0.02413158118724823, -0.04018968343734741, -0.015258369967341423, 0.012909951619803905, -0.01325269415974617, -0.028638005256652832, -0.023522261530160904, -0.004871380515396595, -0.008029050193727016, -0.01619773730635643, 0.051614418625831604, 0.011805560439825058, 0.009285771287977695, -0.031684599816799164, 0.039504196494817734, 0.004668273963034153, -0.014179366640746593, -0.014801380224525928, -0.0064962320029735565, 0.0045159440487623215, 0.027140095829963684, 0.02174508012831211, -0.00022413110127672553, 0.019637851044535637, 0.0037098657339811325, -0.00015887523477431387, -0.0067025115713477135, -0.010866193100810051, -0.0046174973249435425, -0.030313633382320404, 0.029628148302435875, 0.026556164026260376, 0.01483946293592453, 0.00980623159557581, 0.009266730397939682, -0.02095804363489151, 0.003614659421145916, 0.004642885643988848, -0.00866375770419836, -0.009558695368468761, -0.01387470681220293, 0.01767025887966156, -0.027470143511891365, 0.004991975147277117, 0.007127766031771898, 0.03297940269112587, -0.0075593669898808, 0.02267175354063511, 0.006299472413957119, 0.016058100387454033, -0.00035404780646786094, 0.028638005256652832, -0.014445943757891655, 0.0025372435338795185, -0.010180708952248096, -0.01732751540839672, 0.010434592142701149, -0.005322022829204798, -0.015372617170214653, -0.01848268322646618, -0.00032072566682472825, 0.027673250064253807, 0.0011266059009358287, 0.005099875386804342, -0.013455799780786037, -0.007952885702252388, 0.005017363000661135, 0.01353196520358324, -0.014357085339725018, -0.004757133312523365, -0.026149950921535492, -0.018787343055009842, -0.0195870753377676, -0.012656068429350853, -0.0298058670014143, -0.009818925522267818, -0.0033893382642418146, -0.041306767612695694, 0.013087669387459755, -0.009438101202249527, -0.03815861791372299, -0.0007382442126981914, -0.0039923107251524925, -0.014293613843619823, -0.003909798339009285, 0.01665472611784935, -0.0006874676328152418, -0.017378292977809906, -0.015575723722577095, 0.029526595026254654, -0.00433187885209918, -0.00980623159557581, -7.795001874910668e-05, 0.011424736119806767, 0.02504556067287922, -0.021897410973906517, -0.018965061753988266, 0.02152927964925766, 0.010180708952248096, -0.02290024794638157, 0.013684295117855072, -0.00464605912566185, 0.029628148302435875, -0.012814745306968689, 0.01834304817020893, 0.0005474352510645986, 0.012941687367856503, -0.0209453497081995, 0.0034813708625733852, 0.0036622625775635242, 0.006753288209438324, -0.030059749260544777, -0.002162765944376588, -0.02424582839012146, 0.02617533877491951, -0.00980623159557581, -0.010244179517030716, 0.00815599225461483, -0.01002203207463026, -0.013074975460767746, 0.0218720231205225, -0.010923316702246666, 0.008365445770323277, -0.008574899286031723, 0.016362760215997696, -0.0036305272951722145, -0.009704678319394588, 0.01354465913027525, -0.007825944572687149, 0.018076471984386444, 0.019460134208202362, -0.022468646988272667, -0.023522261530160904, 0.0037257333751767874, 0.009419060312211514, 0.027267036959528923, -0.007159501314163208, -0.015207593329250813, -0.017086327075958252, -0.004576241597533226, -0.013620824553072453, -0.0056584179401397705, 0.010434592142701149, 0.005052272230386734, 0.008162339217960835, -0.0007084922981448472, -0.0325985811650753, -0.028155626729130745, 0.00478252163156867, 0.006042415741831064, -0.016045406460762024, -0.021770469844341278, -0.020932655781507492, 0.0015772483311593533, -0.0016066035022959113, 0.024753594771027565, -0.0019850479438900948, -0.01240853313356638, 0.015309146605432034, -0.012725886888802052, 0.00906362384557724, -0.010231485590338707, 0.019104696810245514, -0.025451773777604103, 0.008517775684595108, 0.0032877852208912373, 0.01297976914793253, 0.022468646988272667, -0.008923987857997417, -0.023179519921541214, -0.016362760215997696, 0.0011480272514745593, -0.004274755250662565, 0.018165329471230507, -0.005744103342294693, 0.026023009791970253, 0.00529346102848649, 0.006708859000355005, -0.0063502490520477295, -0.018089165911078453, 0.01949821598827839, -0.017847975715994835, -0.001200390630401671, 0.027267036959528923, -0.01586768962442875, -0.02061530202627182, 0.03985963389277458, 0.008657410740852356, -0.002267492702230811, -0.029425041750073433, 0.009273077361285686, -0.021554669365286827, 0.03589905798435211, -0.014319002628326416, -0.014534803107380867, -0.02323029562830925, 0.011589759960770607, -0.016616644337773323, 0.02038680575788021, 0.006391505245119333, -0.013557353056967258, 0.01641353778541088, 0.013125752098858356, 0.019802875816822052, -0.0014614141546189785, 0.01621043123304844, 0.004769827239215374, -0.013836625032126904, 0.007908456027507782, -0.036178331822156906, -0.027114706113934517, -0.009933173656463623, 0.009685637429356575, -0.004427085164934397, -0.00980623159557581, -0.014166672714054585, 0.00990778487175703, -0.042982395738363266, 0.024677429348230362, -0.010593269020318985, 0.0047856951132416725, 0.011056605726480484, 0.021605445072054863, -0.023598426952958107, 0.00671520596370101, -0.03104989416897297, 0.005813921336084604, -0.005106222350150347, -0.013836625032126904, -0.006569223012775183, 0.0003863782330881804, 0.009425407275557518, -0.009768148884177208, -0.04237307608127594, -0.010999482125043869, 0.008701840415596962, 0.00248011969961226, 0.012040402740240097, -0.006899271160364151, -0.014077813364565372, -0.007045253645628691, 0.0018596930895000696, 0.014445943757891655, 0.03320790082216263, 0.0052744196727871895, -0.0017184707103297114, -0.012776663526892662, -0.015359923243522644, 0.018279576674103737, -0.005756797734647989, -0.04823777452111244, -0.017733728513121605, 0.00922864768654108, -0.010885234922170639, 2.978290922328597e-06, -0.015829605981707573, -0.028231792151927948, 0.01767025887966156, -0.017302127555012703, -0.0026562511920928955, -0.022455953061580658, -0.017238657921552658, 0.01523298118263483, 0.007984621450304985, -0.019066615030169487, 0.00787672121077776, -0.00010601599933579564, -0.01947282813489437, 0.0164389256387949, 0.0071087246760725975, 0.00016889171092770994, -0.009311160072684288, -0.015880383551120758, 0.0049570659175515175, -0.005944036412984133, -0.02097073756158352, 0.0035226268228143454, 0.0039002778939902782, -0.0016113638412207365, 0.004360441118478775, 0.012719538994133472, -0.01489023957401514, 0.012478350661695004, 0.002343657659366727, -0.003776510013267398, 0.00044945228728465736, -0.008797046728432178, 0.015842299908399582, -0.017403680831193924, -0.016743585467338562, -0.005249031353741884, -0.022176681086421013, -0.02060260809957981, 0.018038388341665268, 0.00934289488941431, -0.007648225873708725, 0.025883374735713005, 0.21935492753982544, 0.012015013955533504, -0.005020536947995424, 0.037422358989715576, -0.00042168382788076997, 0.031430717557668686, 0.0018533460097387433, 0.0024769462179392576, -0.017200574278831482, 0.013227305375039577, -0.011532636359333992, -0.006569223012775183, 0.0015851821517571807, -0.003519453341141343, 0.006264563649892807, -0.004874553997069597, -0.025083642452955246, -0.023826921358704567, 0.00023186660837382078, -0.03554362431168556, 0.010707516223192215, -0.016515091061592102, 0.0008505081059411168, -0.01732751540839672, 0.03770162910223007, 0.0020167832262814045, -0.04409947991371155, 0.006562876049429178, 0.013988954946398735, 0.024410853162407875, -0.018965061753988266, -0.00866375770419836, 0.005893259774893522, 0.001650239690206945, -0.014420555904507637, 0.013024198822677135, 0.006943700835108757, 0.003874889574944973, 0.00572188850492239, -0.0038272866513580084, 0.009850661270320415, -0.013113058172166348, 0.017352905124425888, -0.016451619565486908, 0.01732751540839672, 0.02447432279586792, -0.018736567348241806, -0.027267036959528923, 0.0032147937454283237, 0.03648298978805542, -0.015144121833145618, -0.018749261274933815, 0.006477190647274256, 0.010174361988902092, 0.0054553113877773285, 0.009165177121758461, 0.025997621938586235, -0.020209088921546936, -0.019866347312927246, -0.006064630579203367, 0.010644045658409595, 0.009482530876994133, -0.01641353778541088, 0.0008592353551648557, -0.030085138976573944, 0.009590431116521358, -0.014877544716000557, 0.014331696555018425, -0.003222727682441473, -0.016020018607378006, -0.0014169845962896943, -0.024994783103466034, 0.00020846175902988762, 0.022341705858707428, -0.009025541134178638, -0.010072808712720871, 0.02491861768066883, -0.012859174981713295, 0.019523603841662407, 0.03501046821475029, -0.026810046285390854, -4.2148549255216494e-05, -0.006823106203228235, -0.011183546856045723, -0.016921304166316986, -0.03013591468334198, 0.0040716491639614105, 0.014585579745471478, -0.018990449607372284, -0.02074224315583706, 0.011354918591678143, -0.012675110250711441, -0.0001016523819998838, -0.0013352660462260246, 0.017847975715994835, 0.005667938385158777, -0.006569223012775183, 0.010237832553684711, 0.003319520503282547, -0.011361265555024147, -0.010904275812208652, -0.05509261414408684, -0.009457143023610115, -0.004947545472532511, -0.02876494638621807, -0.003143389243632555, 0.0027958867140114307, 0.036762259900569916, 0.003160843625664711, -0.0055346498265862465, -0.002627689391374588, -0.0002687589731067419, 0.017949528992176056, -0.0015145709039643407, 0.0024118886794894934, -0.0026768790557980537, -0.016921304166316986, 0.003541668178513646, 0.030288243666291237, -0.023966556414961815, 0.01880003698170185, -0.019104696810245514, -0.002960910787805915, 0.033385615795850754, -0.02050105482339859, -0.012960728257894516, -0.0115072475746274, 0.011983278207480907, 0.025451773777604103, -0.011881725862622261, 0.038564831018447876, -0.0033893382642418146, -0.007933844812214375, 0.019980594515800476, -0.033715665340423584, -0.018711179494857788, 0.00957773718982935, 0.018152635544538498, -0.026886211708188057, -0.012224467471241951, 0.0248043704777956, 0.0008104422013275325, -0.0010694821830838919, 0.006420067045837641, -0.0050014955922961235, -0.03544206917285919, 0.01235140860080719, 0.0075974492356181145, -0.02990742027759552, -0.018317660316824913, -0.030897563323378563, -0.014509414322674274, -0.017276739701628685, -0.017200574278831482, 0.02493131347000599, -0.020221782848238945, -0.04465802386403084, -0.005667938385158777, -0.01382393017411232, -0.0008481279946863651, -0.0028704649303108454, -0.0064867110922932625, 0.05301077663898468, -0.011621495708823204, 0.008086173795163631, 0.005645723547786474, -0.16146960854530334, 0.009184218011796474, -0.024563182145357132, -0.016388149932026863, 0.015728052705526352, 0.0006854841485619545, 0.023141438141465187, -0.0074260784313082695, -0.018101859837770462, 0.018876202404499054, 0.010244179517030716, 0.010396509431302547, -0.033030182123184204, 0.0023500046227127314, 0.019053921103477478, 0.01654047891497612, 0.0001256522664334625, -0.014407861046493053, 0.023255685344338417, 0.019663240760564804, 0.03838711231946945, 0.002702267374843359, 0.0010972507297992706, -0.00762283755466342, 0.002757804235443473, 0.030516739934682846, -0.025527937337756157, 0.0037796834949404, -0.011043911799788475, 0.001810503308661282, 0.0059535568580031395, -0.0029196548275649548, 0.025540631264448166, -0.008492386899888515, 0.010313997976481915, 0.005436270032078028, 8.990037167677656e-05, -0.006556529086083174, 0.0177972000092268, 0.03526435047388077, 0.013189222663640976, 0.027901744470000267, -0.0013908029068261385, 0.014928321354091167, -0.009768148884177208, 0.009914131835103035, 0.009311160072684288, -0.001729578129015863, 0.023598426952958107, -0.004665100481361151, 0.009450795128941536, -0.037447746843099594, 0.016134265810251236, 0.0011900766985490918, 0.0022341706790030003, 0.016933998093008995, -0.014166672714054585, 0.0014907693257555366, -0.005883738864213228, -0.0006581124034710228, -0.012256203219294548, -0.011342223733663559, -0.013163834810256958, 0.002048518741503358, -0.0012741753598675132, -0.004030392970889807, -0.006331207696348429, -0.01489023957401514, 0.0064644962549209595, 0.0060202013701200485, -0.002526136115193367, 0.002645143773406744, 0.0033702971413731575, 0.0074832020327448845, 6.649553688475862e-05, 0.023496873676776886, -0.017644869163632393, -0.006518446374684572, 0.021808551624417305, -0.02323029562830925, -0.015106040053069592, 0.034172654151916504, -0.025997621938586235, 0.021338868886232376, -0.015702664852142334, 0.0022373441606760025, 0.0041414666920900345, 0.01495371013879776, 0.014204755425453186, -0.007451466750353575, 0.007337219547480345, -0.0035448416601866484, -0.009323853999376297, -0.02560410276055336, 0.018089165911078453, 0.011018523015081882, 0.009025541134178638, 0.010491715744137764, -0.00580440042540431, -0.003693997859954834, 0.0055790795013308525, -0.02152927964925766, -0.028714170679450035, 0.02559140883386135, 0.016362760215997696, 0.033588722348213196, 0.012433920986950397, 0.0270893182605505, 0.01949821598827839, -0.005153825506567955, -0.02006945200264454, 0.010263221338391304, 0.019904429093003273, 0.025464467704296112, -0.010726558044552803, 0.038818713277578354, -0.014991792850196362, -0.025413690134882927, 0.030415186658501625, -0.009108053520321846, 0.046612922102212906, 0.0008076653466559947, 0.0016772147500887513, 0.012890910729765892, -0.0007556987111456692, 0.0033290411811321974, -0.10795105993747711, -0.022925637662410736, 0.020551830530166626, 0.020475665107369423, -0.00628677848726511, -0.0052522048354148865, 0.009368283674120903, 0.013455799780786037, -0.014344390481710434, 0.0036908243782818317, -0.011221629567444324, -0.018038388341665268, -0.015283757820725441, -0.013608129695057869, 0.023738062009215355, 0.008454304188489914, 0.02515980787575245, -0.02866339311003685, -0.00038320469320751727, 0.0051728663966059685, -0.0007136493222787976, 0.011431083083152771, -0.0049792807549238205, 0.006772329565137625, -0.004312837962061167, 0.007495896425098181, -0.00900015328079462, 0.02990742027759552, 0.0072229718789458275, 0.01127875316888094, 0.024182358756661415, -0.009939520619809628, 0.01734021119773388, -0.004036739934235811, -0.011659577488899231, -0.0005188734503462911, -0.02469012327492237, 0.0023801533970981836, 0.014725214801728725, -0.026987764984369278, 0.012770315632224083, -0.007451466750353575, -0.00236587249673903, -0.04757767915725708, 0.006867535877972841, -0.004471514839679003, 0.0003590064588934183, 0.018673095852136612, 0.022951025515794754, -0.0070198653265833855, -0.0037701628170907497, 0.025781821459531784, -0.029653536155819893, 0.00464605912566185, 0.04166220501065254, -0.005880565382540226, 0.01664203219115734, -0.0018581063486635685, -0.00974276103079319, -0.006594611331820488, 0.006753288209438324, -0.0011456471402198076, -0.020209088921546936, 0.007933844812214375, 0.008257545530796051, 0.015943853184580803, -0.011069299653172493, -0.0024150621611624956, 0.005141131114214659, -0.01518220454454422, 0.01207213755697012, -0.006499405484646559, -0.016616644337773323, -0.008930335752665997, -0.010079155676066875, -0.038691774010658264, -0.03409649059176445, 0.005633029621094465, -6.599966582143679e-05, -0.02264636568725109, 0.00196918030269444, -0.00974276103079319, 0.007419731467962265, -0.0011853163596242666, -0.0006704098777845502, -0.020132923498749733, -0.010206097736954689, -0.0067342473194003105, -0.0074832020327448845, -0.015816912055015564, -0.004369961563497782, 0.011240670457482338, 0.031100669875741005, -0.01364621240645647, -0.016933998093008995, 0.01812724769115448, -0.014407861046493053, -0.0021215099841356277, 0.017390986904501915, -0.023623814806342125, -0.006277257576584816, -0.01013627927750349, -0.04018968343734741, 0.006899271160364151, -0.017847975715994835, -0.0068040648475289345, 0.008758964017033577, -0.010009338147938251, 0.016743585467338562, 0.010091850534081459, -0.008295627310872078, 0.01701016165316105, -0.01059961598366499, 0.012694151140749454, 0.013747765682637691, 0.007971926592290401, -0.017187880352139473, -0.008860517293214798, 0.015398005023598671, 0.0051950812339782715, 0.022354399785399437, -0.009190564975142479, 0.02221476472914219, 0.03036440908908844, -0.004541332367807627, -0.0006462116143666208, -0.02038680575788021, 0.021884717047214508, -0.013290775939822197, 0.016857832670211792, -0.027597084641456604, -0.010974093340337276, 0.01155802421271801, -0.024563182145357132, 0.0033322146628051996, 0.02607378549873829, -0.014471332542598248, -0.030059749260544777, -0.004170028492808342, 0.012224467471241951, 0.009647554717957973, 0.0027133747935295105, -0.006613652687519789, -0.03668609634041786, 0.010974093340337276, -0.023712674155831337, -0.011919807642698288, 0.014979097992181778, -0.016032712534070015, -0.004030392970889807, 0.018774649128317833, 0.003919319249689579, -0.008289280347526073, 0.0111518120393157, 0.010504409670829773, -0.019231639802455902, 0.015778830274939537, 0.00135192705783993, 0.0059662507846951485, -0.011849990114569664, 0.005096701439470053, -0.031075282022356987, 0.03589905798435211, -0.009076317772269249, 0.008816087618470192, -0.009139788337051868, -0.0047063566744327545, -0.013608129695057869, -0.016718197613954544, -0.01365890633314848, 0.014712520875036716, -0.029882032424211502, -0.018622320145368576, 0.005153825506567955, 0.019853651523590088, 0.01121528260409832, 0.039250314235687256, -0.017936835065484047, -0.006956394761800766, -0.028612617403268814, -0.008308322168886662, 0.010859846137464046, 0.013582741841673851, 0.019866347312927246, -0.024398159235715866, 0.010859846137464046, 0.010288609191775322, 0.02470281720161438, -0.01665472611784935, -0.0012868695193901658, 0.0054426174610853195, 0.004649232607334852, -0.004687315318733454, -0.01330347079783678, 0.011227976530790329, -0.0015740747330710292, 0.003966922406107187, 0.025299442932009697, -0.0030148609075695276, -0.007902109064161777, 0.017860671505331993, 0.03127838671207428, -0.005366452503949404, 0.004652406554669142, 0.003019621130079031, -0.007159501314163208, -0.012015013955533504, -0.016223125159740448, -0.033918771892786026, -0.042880840599536896, 0.009108053520321846, 0.00739434314891696, 0.01824149489402771, 0.016730891540646553, -0.0039923107251524925, 0.022633671760559082, 0.0074387723580002785, 0.012091179378330708, -0.0013773153768852353, -0.005832962226122618, -0.019015837460756302, 0.026886211708188057, 0.003041835967451334, 0.012141955085098743, 0.007857679389417171, -0.020462971180677414, 0.0060900188982486725, 0.01721326820552349, -0.0041192518547177315, 0.00015084221377037466, 0.0010599616216495633, 0.005166519433259964, -0.0015613805735483766, 0.009507918730378151, -0.01629929058253765, -0.010364774614572525, -0.014496720395982265, 0.007102377712726593, 0.015994630753993988, 0.01880003698170185, -0.025185195729136467, 0.036889202892780304, 0.004452473483979702, 0.008708187378942966, 0.003944707568734884, -0.007864026352763176, 0.005382319912314415, 0.028028685599565506, -0.020107535645365715, -0.0063058193773031235, 0.004541332367807627, 0.02446162886917591, 0.007572060916572809, 0.0057377563789486885, -0.012491044588387012, -0.02617533877491951, -0.003003753488883376, 0.0003453205863479525, 0.0008489213651046157, -0.028917275369167328, -0.023395320400595665, 0.008689146488904953, 0.01868578977882862, 0.014369779266417027, 0.028257180005311966, -0.013988954946398735, -0.003121174406260252, 0.001888255006633699, -0.003398858942091465, 0.012986116111278534, -0.04090055450797081, 0.0025911936536431313, -0.0018596930895000696, -0.04950718954205513, -0.008860517293214798, -0.004576241597533226, -0.021846633404493332, -0.014039730653166771, -0.01546147558838129, 0.0040716491639614105, 0.0027324159163981676, -0.03135455399751663, 0.04112904891371727, -0.017606787383556366, -0.018635014072060585, 0.014534803107380867, 0.01048536878079176, -0.015423393808305264, -0.01937127485871315, -0.002765738172456622], index=0, object='embedding')], model='text-embedding-ada-002', object='list', usage=Usage(prompt_tokens=8, total_tokens=8)) |
可以看到所得到嵌入向量完全不适合人类阅读,这正是大语言的奇妙之处。因为最适合计算机理解的是数字,通过将自然语言转换为数字向量,计算机就可以更高效地去推理其中的关系,所得到的向量会做为机器学习模型及自然语言处理算法的输入。
token是一种分词方式,OpenAI已经开源了这部分代码,因此无需调用 API,安装tiktoken
包即可查看:
1 2 3 4 |
import tiktoken enc = tiktoken.get_encoding("cl100k_base") enc.encode('The food was delicious and the waiter...') |
执行结果为:
1 |
[791, 3691, 574, 18406, 323, 279, 68269, 1131] |
如何知道是cl100k_base
呢?一方面是查阅官方文档,另外tiktoken
内置了通过模型名称查询编码方式的方法:
1 2 |
encoding = tiktoken.encoding_for_model('gpt-3.5-turbo') print(encoding) |
调用DALL·E实现文生图
1 2 3 4 5 |
response = client.images.generate( prompt="A cute baby sea otter", n=2, size="1024x1024" ) |
client.images.generate有多个参数,这里使用了提示词prompt
,生成图片数量n
(取值范围为1到10,DALL·E 3当前仅支持使用1生成一张图)size
是图像的尺寸的参数。通过IPython.display可在Jupyter Notebook中显示所生成图片,我们生成了两张,遍历显示如下:
API中的角色
在前面的示例中已经出现过role
,当前共有三种角色:
- system:用于设置 AI 的行为、背景等,比如设定其为人工智能专家等。
- assistant:通常是模型的回复,可用于提供上下文。
- user:模型的使用者,也即聊天内容的发起者。
如下面的示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
completion = client.chat.completions.create( # For GPT 3.5 Turbo, the model is "gpt-3.5-turbo" model="gpt-3.5-turbo", # Conversation as a list of messages. messages=[ {"role": "system", "content": "You are a helpful teacher."}, { "role": "user", "content": "Are there other measures than time complexity for an algorithm?", }, { "role": "assistant", "content": "Yes, there are other measures besides time complexity for an algorithm, such as space complexity.", }, {"role": "user", "content": "What is it?"}, ], ) print(completion.choices[0].message.content) |
输出结果为:
1 2 3 4 5 6 7 |
Space complexity refers to the amount of memory or storage space required by an algorithm to solve a problem. It measures the maximum amount of space used by an algorithm as a function of the input size. Just like time complexity, space complexity is also analyzed in terms of Big O notation. Space complexity is important because it determines how efficiently an algorithm utilizes memory resources. It can be crucial in scenarios with limited memory, such as embedded systems or applications running on devices with low memory capacity. Similar to time complexity, there are different types of space complexity, such as auxiliary space complexity and total space complexity. Auxiliary space complexity refers to the extra space used by an algorithm, excluding the input space, while total space complexity includes both the auxiliary space and the input space. Analyzing space complexity can help in optimizing algorithms by identifying ways to reduce memory usage, such as reusing variables or minimizing the number of data structures created during the execution of an algorithm. |
函数调用
函数调用为 OpenAI扩展了数据来源,同时也让开发者能在利用大语言模型优秀的自然语言理解能力的同时向其投喂数据或添加逻辑。下面举一个例子,其中并未涉及到外部 API,旨在探讨函数调用,今后我们再做深入的展开。
首先定义一个函数模型对数据库执行搜索:
1 2 3 4 5 6 7 8 |
# Example function def find_product(sql_query): # Execute query here results = [ {"name": "pen", "color": "blue", "price": 1.99}, {"name": "pen", "color": "red", "price": 1.78}, ] return results |
接着对函数的详情进行描述,主要包含名称、描述、参数的定义,其中参数有类型、属性和必填项的指定:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# Function definition functions = [ { "name": "find_product", "description": "Get a list of products from a sql query", "parameters": { "type": "object", "properties": { "sql_query": { "type": "string", "description": "A SQL query", } }, "required": ["sql_query"], }, } ] |
创建一个会话:
1 2 3 4 5 6 7 8 9 |
# Example question user_question = "I need the top 2 products where the price is less than 2.00" messages = [{"role": "user", "content": user_question}] # Call the openai.ChatCompletion endpoint with the function definition response = client.chat.completions.create( model="gpt-3.5-turbo-0613", messages=messages, functions=functions ) response_message = response.choices[0].message messages.append(response_message) |
此时message
为:
1 |
[{'role': 'user', 'content': 'I need the top 2 products where the price is less than 2.00'}, ChatCompletionMessage(content=None, role='assistant', function_call=FunctionCall(arguments='{\n "sql_query": "SELECT * FROM products WHERE price < 2.00 ORDER BY price LIMIT 2"\n}', name='find_product'), tool_calls=None)] |
response_message
为:
1 |
ChatCompletionMessage(content=None, role='assistant', function_call=FunctionCall(arguments='{\n "sql_query": "SELECT * FROM products WHERE price < 2.00 ORDER BY price LIMIT 2"\n}', name='find_product'), tool_calls=None) |
大语言模型为我们创建了查询语句,打印response_message.function_call
可得:
1 |
FunctionCall(arguments='{\n "sql_query": "SELECT * FROM products WHERE price < 2.00 ORDER BY price LIMIT 2"\n}', name='find_product') |
接下执行函数继续会话:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import json # Call the function function_args = json.loads( response_message.function_call.arguments ) products = find_product(function_args.get("sql_query")) # Append the function's response to the messages messages.append( { "role": "function", "name": "find_product", "content": json.dumps(products), } ) # Format the function's response into natural language response = client.chat.completions.create( model="gpt-3.5-turbo-0613", messages=messages, ) |
得到的回复如下:
1 2 3 4 |
The top 2 products where the price is less than $2.00 are: 1. Pen (Blue) - Price: $1.99 2. Pen (Red) - Price: $1.78 |
内容审核
我们知道大语言模型应用面临的一大问题就是监管,在ChatGPT中有大量信息过滤处理,但很多并没有体现在 API 上,因此OpenAI额外提供了Moderation 的 API,供我们调用:
1 2 3 4 5 |
response = client.moderations.create( model="text-moderation-latest", input="I want to kill my neighbor.", ) print(response) |
可以在结果中发现harassment_threatening=True
、violence=True
和harassment/threatening=True
。
同时在使用 ChatGPT 等人工智能聊天工具还需要关注其胡编乱造、无中生有的可能性,出即所谓“幻觉”(hallucination)。
OpenAI所提供的功能还有很多有待探讨,本文旨在做一个入门介绍,希望后续能有机会做更深入的分享。