400-638-8808
|
微信公眾號





穩(wěn)定可靠 永不間斷

海外收發(fā) 暢通無阻

協(xié)同辦公 資源管理

超大郵件 超級功能

智能反垃圾郵件技術(shù)
易管理 免維護(hù)

- platform.openai.com/examples
- platform.openai.com/docs/api-re…
ChatGPT可以實(shí)現(xiàn)chat,生成圖片,識別關(guān)鍵,改錯等等功能,本文簡單介紹如何使用python調(diào)用ChatGPT API 接口。

從openai官網(wǎng)網(wǎng)址:platform.openai.com/account/api…,生成我們的API key:

獲得key后我們就可以調(diào)用API接口了。
使用pip安裝openai庫,命令: pip install openai。
安裝openai庫: pip install openai
# 調(diào)用openai api的步驟 # 1. 安裝openai庫 pip install openai # 2. 設(shè)置openai的api_key # 3. 調(diào)用openai的api # 4. 參考文檔 # https://platform.openai.com/docs/api-reference/completions/create # https://platform.openai.com/docs/api-reference/authentication # https://platform.openai.com/docs/api-reference/completions/create # https://platform.openai.com/docs/libraries/community-libraries
API可以實(shí)現(xiàn)chat,生成圖片,識別關(guān)鍵,改錯等等功能。 下面是主要示例代碼。 注意:openai.api_key = "sk-xxxFQ" #要更換成自已的API KEY
#!/usr/local/bin/python3.8 # -*- coding: utf8 -*- # 調(diào)用openai api的步驟 # 1. 安裝openai庫 pip install openai # 2. 設(shè)置openai的api_key # 3. 調(diào)用openai的api # 4. 參考文檔 # https://platform.openai.com/docs/api-reference/completions/create # https://platform.openai.com/docs/api-reference/authentication # https://platform.openai.com/docs/api-reference/completions/create # https://platform.openai.com/docs/libraries/community-libraries import os import openai import json # 1. 準(zhǔn)備好請求的url #openai.organization = "YOUR_ORG_ID" # #openai.api_key = os.getenv("OPENAI_API_KEY") openai.api_key = "sk-xxxFQ" #要更換成自已的API KEY # 查看可以使用的模型列表 def get_model_list(): models= openai.Model.list() print(models) # 生成文本示例 def generate_text(prompt): completions = openai.Completion.create( engine="text-davinci-002", prompt=prompt, max_tokens=1024, n=1, stop=None, temperature=0.5, ) message = completions.choices[0].text return message.strip() # 調(diào)用openai 畫圖示例 def generate_image(prompt): response = openai.Image.create( prompt = prompt, n=1, size="512x512" ) image_url = response["data"
][0]["url"] return image_url # 調(diào)用openai 問答示例 def chat(prompt): response = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=[ {"role": "user", "content":prompt} ] ) answer = response.choices[0].message.content return answer # 調(diào)用openai 改正錯詞輸出正確句子 def correct(): prompt="改正錯詞輸出正確句子:\n\n我在京東電商平臺買了蘋果耳幾和華為體脂稱" #建議prompt: 改正錯詞輸出正確句子:\n\n input_sentence response = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=[ {"role": "user", "content":prompt} ] ) answer = response.choices[0].message.content return answer # 調(diào)用openai 識別關(guān)鍵詞 def keyword(): prompt="對下面內(nèi)容識別2個關(guān)鍵詞,每個詞字?jǐn)?shù)不超過3個字:\n\n齊選汽車掛件車內(nèi)掛飾車載后視鏡吊墜高檔實(shí)心黃銅玉石出入平安保男女 紅流蘇-玉髓平安扣" #建議prompt: 對下面內(nèi)容識別n個關(guān)鍵詞,每個詞字?jǐn)?shù)不超過m個字:\n\n input data response = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=[ {"role": "user", "content":prompt} ] ) answer = response.choices[0].message.content return answer # 抽取文本向量 (Embedding) def embedding(): content = "蘋果手機(jī)" response = openai.Embedding.create( model="text-embedding-ada-002", input=content ) answer = response.data[0].embedding return answer def api_test(): # 測試chat # prompt = "人口最多的國家?" # response = chat(prompt) # print(response) # # 測試generate_text # prompt = "Hello, how are you today?" # response = generate_text(prompt) # print(response) # 測試generate_image #prompt = "a delicious dessert" #response = generate_image(prompt) #print(response) # 測試correct # response = correct() # print(response) #輸出結(jié)果: 我在京東電商平臺買了蘋果耳機(jī)和華為體脂秤。 # 測試keyword #response = keyword() #print(response) #輸出結(jié)果: 掛件、平安扣 # 測試embedding result = embedding() print(len(result)) print(result) if __name__ == "__main__": api_test()
github.com/openai/open…
下載git代碼:
git clone https://github.com/openai/openai-quickstart-python.git cd openai-quickstart-python cp .env.example .env python -m venv venv . venv/bin/activate pip install -r requirements.txt flask run
運(yùn)行效果:

<
產(chǎn)品與服務(wù)
香港服務(wù)器 香港高防服務(wù)器 美國服務(wù)器 韓國服務(wù)器 新加坡服務(wù)器 日本服務(wù)器 臺灣服務(wù)器云服務(wù)器
香港云主機(jī) 美國云主機(jī) 韓國云主機(jī) 新加坡云主機(jī) 臺灣云主機(jī) 日本云主機(jī) 德國云主機(jī) 全球云主機(jī)高防專線
海外高防IP 海外無限防御 SSL證書 高防CDN套餐 全球節(jié)點(diǎn)定制 全球?qū)>GPLC關(guān)于我們
關(guān)于天下數(shù)據(jù) 數(shù)據(jù)招商加盟 天下數(shù)據(jù)合作伙伴 天下數(shù)據(jù)團(tuán)隊建設(shè) 加入天下數(shù)據(jù) 媒體報道 榮譽(yù)資質(zhì) 付款方式關(guān)注我們
微信公眾賬號
新浪微博
天下數(shù)據(jù)手機(jī)站 關(guān)于天下數(shù)據(jù) 聯(lián)系我們 誠聘英才 付款方式 幫助中心 網(wǎng)站備案 解決方案 域名注冊 網(wǎng)站地圖
天下數(shù)據(jù)18年專注海外香港服務(wù)器、美國服務(wù)器、海外云主機(jī)、海外vps主機(jī)租用托管以及服務(wù)器解決方案-做天下最好的IDC服務(wù)商
《中華人民共和國增值電信業(yè)務(wù)經(jīng)營許可證》 ISP證:粵ICP備07026347號
朗信天下發(fā)展有限公司(控股)深圳市朗玥科技有限公司(運(yùn)營)聯(lián)合版權(quán)
深圳總部:中國.深圳市南山區(qū)深圳國際創(chuàng)新谷6棟B座10層 香港總部:香港上環(huán)蘇杭街49-51號建安商業(yè)大廈7樓
7×24小時服務(wù)熱線:4006388808香港服務(wù)電話:+852 67031102
本網(wǎng)站的域名注冊業(yè)務(wù)代理北京新網(wǎng)數(shù)碼信息技術(shù)有限公司的產(chǎn)品