
如何快速實(shí)現(xiàn)REST API集成以優(yōu)化業(yè)務(wù)流程
記錄該密鑰或在安全的地方保存它。基于安全原因,它將不會(huì)從 OpenAI 賬戶部分再次可見。而且不要與任何人分享此密鑰。如果你計(jì)劃使用企業(yè)解決方案,請(qǐng)向你的組織查詢 API 密鑰。由于該密鑰與你的付費(fèi) OpenAI 計(jì)劃相關(guān),因此請(qǐng)謹(jǐn)慎使用。
本指南使用 Python 編程語(yǔ)言來(lái)調(diào)用 OpenAI API 密鑰。你可以使用 Java 或其他任何語(yǔ)言來(lái)調(diào)用它。
首先,請(qǐng)確保你在 Linux 或 Windows 中已經(jīng)安裝了 Python。如果沒有,請(qǐng)按照以下指南安裝 Python。如果你使用現(xiàn)代 Linux 發(fā)行版(例如 Ubuntu),Python 應(yīng)該已經(jīng)安裝好了。
在安裝 Python 后,確保 pip
在 Linux 發(fā)行版中可用。運(yùn)行以下命令進(jìn)行安裝。對(duì)于 Windows,你應(yīng)該已經(jīng)在 Python 安裝的一部分中安裝了它。
Ubuntu、Debian 和其他基于 Debian 的發(fā)行版:
sudo apt install python3-pip
Fedora、RHEL、CentOS 等:
sudo dnf install python3-pip
Arch Linux:
sudo pacman -S python-pip
上述步驟中創(chuàng)建的 API 密鑰,你可以直接在程序中使用。但這并不是最佳實(shí)踐。
最佳實(shí)踐是從文件或你系統(tǒng)的環(huán)境變量中調(diào)用它。
對(duì)于 Windows,請(qǐng)?jiān)O(shè)置一個(gè)任何名字的環(huán)境變量,例如 API-KEY
。并添加密鑰值。
對(duì)于 Linux,請(qǐng)使用超級(jí)用戶權(quán)限打開 /etc/environment
文件并添加密鑰。例如:
API-KEY="<你的密鑰>"
對(duì)于基于文件的密鑰訪問(wèn),請(qǐng)?jiān)谀愕拇a中使用以下語(yǔ)句:
openai.api_key_path = <你的 API 密鑰路徑>
對(duì)于直接在代碼中訪問(wèn)(不建議),你可以在你的代碼中使用以下語(yǔ)句:
openai.api_key = "你的密鑰"
注意:如果驗(yàn)證失敗,OpenAI API 將拋出以下錯(cuò)誤。你需要驗(yàn)證你的密鑰值、路徑和其他參數(shù)以進(jìn)行更正:openai.error.AuthenticationError: No API key provided
。
最后一步是安裝 OpenAI 的 Python 庫(kù)。打開終端或命令窗口,使用以下命令安裝 OpenAI API。
pip install openai
在此階段,你已經(jīng)準(zhǔn)備好撰寫你的第一個(gè)程序了!
OpenAI API 提供了各種接口模式。例如“聊天補(bǔ)完”、“代碼補(bǔ)完”、“圖像生成”等。在本指南中,我將使用 API 的“聊天補(bǔ)完”功能。使用此功能,我們可以創(chuàng)建一個(gè)簡(jiǎn)單的對(duì)話聊天機(jī)器人。
首先,你需要導(dǎo)入 OpenAI 庫(kù)。你可以使用以下語(yǔ)句在你的 Python 程序中完成:
import openai
在這個(gè)語(yǔ)句之后,你應(yīng)該確保啟用你的 API 密鑰。你可以使用上面解釋的任何方法來(lái)完成。
openai.api_key="your key here"
openai.api_key="your environment variable"
openai.api_key_path = <your path to API key>
OpenAI 聊天 API 的基本功能如下所示。openai.ChatCompletion.create
?函數(shù)以 JSON 格式接受多個(gè)參數(shù)。這些參數(shù)的形式是 “角色”(role
) 和 “內(nèi)容”(content
):
openai.ChatCompletion.create(
model = "gpt-3.5-turbo",
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Who won the world series in 2020?"},
{"role": "assistant", "content": "The Los Angeles Dodgers won the World Series in 2020."},
{"role": "user", "content": "Where was it played?"}
]
)
說(shuō)明:
role
: 有效的值為?system
、user
、assistant
??system
: 指示 API 如何行動(dòng)。基本上,它是 OpenAI 的主提示。
??user
: 你要問(wèn)的問(wèn)題。這是單個(gè)或多個(gè)會(huì)話中的用戶輸入。它可以是多行文本。
??assistant
: 當(dāng)你編寫對(duì)話時(shí),你需要使用此角色來(lái)添加響應(yīng)。這樣,API 就會(huì)記住討論的內(nèi)容。
注意:在一個(gè)單一的消息中,你可以發(fā)送多個(gè)角色。如上述代碼片段所示的行為、你的問(wèn)題和歷史記錄。
讓我們定義一個(gè)數(shù)組來(lái)保存 OpenAI 的完整消息。然后向用戶展示提示并接受?system
?指令。
messages = []
system_message = input("What type of chatbot you want me to be?")
messages.append({"role":"system","content":system_message})
一旦設(shè)置好了,再次提示用戶進(jìn)行關(guān)于對(duì)話的進(jìn)一步提問(wèn)。你可以使用 Python 的?input
?函數(shù)(或任何其他文件輸入方法),并為角色?user
?設(shè)置?content
。
print("Alright! I am ready to be your friendly chatbot" + "\n" + "You can now type your messages.")
message = input("")
messages.append({"role":"user","content": message})
現(xiàn)在,你已經(jīng)準(zhǔn)備好了具有基本 JSON 輸入的數(shù)組,用于調(diào)用“聊天補(bǔ)完”服務(wù)的 create
函數(shù)。
response=openai.ChatCompletion.create(model="gpt-3.5-turbo",messages=messages)
現(xiàn)在,你可以對(duì)其進(jìn)行適當(dāng)?shù)母袷交创蛴№憫?yīng),要么解析響應(yīng)。響應(yīng)是以 JSON 格式提供的。輸出響應(yīng)提供了 choices
數(shù)組。響應(yīng)在 message
JSON 對(duì)象下提供,其中包括 content
值。
對(duì)于此示例,我們可以讀取?choices
?數(shù)組中的第一個(gè)對(duì)象并讀取其?content
。
reply = response["choices"][0]["message"]["content"]
print(reply)
最后,它將為你提供來(lái)自 API 的輸出。
你可以從你的?喜好的 Python IDE?或直接從命令行運(yùn)行代碼。
python OpenAIDemo2.py
以下是使用未格式化的 JSON 輸出運(yùn)行上述程序供你參考。正如你所看到的,響應(yīng)
[debugpoint@fedora python]$ python OpenAIDemo2.py
What type of chatbot you want me to be?a friendly friend
Alright! I am ready to be your friendly chatbot
You can now type your messages.
what do you think about kindness?
{
"choices": [
{
"finish_reason": "stop",
"index": 0,
"message": {
"content": "As an AI language model, I don't have personal opinions, but I can tell you that kindness is a very positive and essential trait to have. Kindness is about being considerate and compassionate towards others, which creates positive emotions and reduces negativity. People who are kind towards others are more likely to inspire kindness and compassion in return. It is an important quality that helps to build positive relationships, promote cooperation, and create a more peaceful world.",
"role": "assistant"
}
}
],
"created": <removed>,
"id": "chatcmpl-<removed>",
"model": "gpt-3.5-turbo-0301",
"object": "chat.completion",
"usage": {
"completion_tokens": 91,
"prompt_tokens": 22,
"total_tokens": 113
}
}
這是一個(gè)適當(dāng)?shù)膶?duì)話式輸出。
[debugpoint@fedora python]$ python OpenAIDemo2.py
What type of chatbot you want me to be?a friendly friend
Alright! I am ready to be your friendly chatbot
You can now type your messages.
what do you think about artificial general intelligence?
As an AI language model, I am programmed to be neutral and not have personal opinions. However, artificial general intelligence (AGI) is a fascinating field of study. AGI refers to the development of machines and algorithms that can perform any intellectual task that a human being can. The potential benefits and risks of AGI are still widely debated, with some experts worried about the implications of machines reaching human-like intelligence. However, many believe that AGI has the potential to revolutionize fields such as healthcare, education, and transportation. The key is to ensure that AGI is developed in a responsible and ethical manner.
這是上面演示中使用的完整代碼。
import openai
openai.api_key = "<your key>"
messages = []
system_message = input("What type of chatbot you want me to be?")
messages.append({"role":"system","content":system_message})
print("Alright! I am ready to be your friendly chatbot" + "\n" + "You can now type your messages.")
message = input("")
messages.append({"role":"user","content": message})
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=messages
)
reply = response["choices"][0]["message"]["content"]
print(reply)
希望這篇簡(jiǎn)單的指南能讓你開始嘗試 OpenAI ChatGPT API。你可以將上述步驟擴(kuò)展到更復(fù)雜的會(huì)話式聊天機(jī)器人。此外,你還可以使用 OpenAI 的其他產(chǎn)品。
請(qǐng)不要錯(cuò)過(guò)我后續(xù)的教程,我將會(huì)實(shí)驗(yàn)和分享給大家。最后,請(qǐng)不要忘記關(guān)注我們,以便及時(shí)獲取我們的文章。
如果上述步驟對(duì)你有幫助,請(qǐng)?jiān)谠u(píng)論框中告訴我。
干杯!
文章轉(zhuǎn)自微信公眾號(hào)@Linux中國(guó)
對(duì)比大模型API的內(nèi)容創(chuàng)意新穎性、情感共鳴力、商業(yè)轉(zhuǎn)化潛力
一鍵對(duì)比試用API 限時(shí)免費(fèi)