Official document:https://platform.openai.com/docs/api-reference/chat/create
1. Use APIfox to call APIs
2.Use PyCharm to call?APIs
2.1-1 WIN OS.Configure the?Enviorment variable
#HK代理環境,不需要科學上網(價格便宜、有安全風險,適合個人開發調試)
setx OPENAI_BASE_URL "https://api.openai-hk.com/v1"
setx OPENAI_API_KEY "hk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"#官方環境,需要科學上網(價格高、安全可靠,適合個人企業生產部署)
setx OPENAI_BASE_URL "https://api.openai.com/v1"
setx OPENAI_API_KEY "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
Temporaily use the API through HK proxy.
Check if environment variable?is configured successfully.
import osprint(os.getenv('OPENAI_BASE_URL'))
2.1-2 MAC OS Configure the Environment variable
#HK代理環境,不需要科學上網
export OPENAI_BASE_URL='https://api.openai-hk.com/v1'
export OPENAI_API_KEY='hk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'#官方環境,需要科學上網
export OPENAI_BASE_URL='https://api.openai.com/v1'
export OPENAI_API_KEY='sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
?if Mac os is unable to read the configured environment variables,u need to modify the .zshrc file.
Refer to the URL below.
MacOS環境變量source生效但重啟后又失效 - 程序員小藝 - 博客園
2.2 Call an API using PyCharm
2.2.1.as APIfox(HTTPClient):Make a POST request to call?the API,similar to how it's done in APIfox.
import requests
import json
import osurl = os.getenv('OPENAI_BASE_URL') + "/chat/completions"
# print(os.getenv('OPENAI_BASE_URL'))
payload = json.dumps({"model": "gpt-4o","messages": [{"role": "system", "content": "assistant"},{"role": "user", "content": "Hello"}]
})
headers = {'Content-Type': 'application/json','Authorization': 'Bearer ' + os.getenv('OPENAI_API_KEY'),
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
2.2.2 Use the?offical SDK to call the API
First,domwload the OpenAI package using?the console and import it.
# pip install openai==1.40.3
Call the API using the SDK
from openai import OpenAI
# pip install openai==1.40.3
import os
# 從環境變量中讀取OPENAI_BASE_URL
print(os.getenv('OPENAI_BASE_URL'))
# 初始化 OpenAI 服務。
client = OpenAI()
completion = client.chat.completions.create(model="gpt-4o",messages=[{"role": "system", "content": "assistant"},{"role": "user", "content": "Hello"}]
)
print(completion.choices[0].message.content)
There was a problem when running the code
Traceback (most recent call last):
? File "D:\BaiduNetdiskDownload\AIapp\sub\1_AI及LLM基礎\day02_OpenAI 開發\day2-demo\sdk_call.py", line 7, in <module>
? ? clien = OpenAI()
? File "C:\Users\Administrator\PycharmProjects\PythonProject\.venv_learn\Lib\site-packages\openai\_client.py", line 123, in __init__
? ? super().__init__(
? ? ~~~~~~~~~~~~~~~~^
? ? ? ? version=__version__,
? ? ? ? ^^^^^^^^^^^^^^^^^^^^
? ? ...<6 lines>...
? ? ? ? _strict_response_validation=_strict_response_validation,
? ? ? ? ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
? ? )
? ? ^
? File "C:\Users\Administrator\PycharmProjects\PythonProject\.venv_learn\Lib\site-packages\openai\_base_client.py", line 843, in __init__
? ? self._client = http_client or SyncHttpxClientWrapper(
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ~~~~~~~~~~~~~~~~~~~~~~^
? ? ? ? base_url=base_url,
? ? ? ? ^^^^^^^^^^^^^^^^^^
? ? ...<5 lines>...
? ? ? ? follow_redirects=True,
? ? ? ? ^^^^^^^^^^^^^^^^^^^^^^
? ? )
? ? ^
? File "C:\Users\Administrator\PycharmProjects\PythonProject\.venv_learn\Lib\site-packages\openai\_base_client.py", line 741, in __init__
? ? super().__init__(**kwargs)
? ? ~~~~~~~~~~~~~~~~^^^^^^^^^^
TypeError: Client.__init__() got an unexpected keyword argument 'proxies'
?The error could be caused by an outdated SDK versiong,trying to update SDK version
pip install --upgrade openai
After updating the SKD ,the code now runs successfully.
?SDK provide a more convenient interface;under the hood,they rely on the request libary for making HTTP request
2.3 Call the embedding(嵌入 向量數據庫) API in Python
embedding transforms prompt words into vector representations(向量表示)
2.3.1 request(HTTP)
One advantage of using the request library is that it provides access to the full response,including headers,status code and body.
import requests
import json
import osurl = os.getenv('OPENAI_BASE_URL') + "/embeddings"payload = json.dumps({"model": "text-embedding-ada-002","input": "cat"
})
headers = {'Content-Type': 'application/json','Authorization': 'Bearer ' + os.getenv('OPENAI_API_KEY'),
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
2.3.2 SDK
Meanwhile,SDK typically provide only pre-processed or pre-packaged object,which may limit access to the raw response date(原始數據). Tip:press Ctrl+B jump to the method
from openai import OpenAI# 初始化 OpenAI 服務。
client = OpenAI()# 調用嵌入 API
def get_embedding(text, model="text-embedding-ada-002"):response = client.embeddings.create(input=text,model=model)return response.data[0].embedding# 示例文本
text = "Hello, world!"# 獲取嵌入向量
embedding = get_embedding(text)print("Embedding vector:", embedding)
2.4 Call the vision-endabled GPT4o using a local/online image
2.4.1 request local image
use #?to comment code
import os
import base64 #use base64 transform image
import requestsdef encode_image(image_path):with open(image_path, "rb") as image_file:return base64.b64encode(image_file.read()).decode('utf-8')# Path to your image
image_path = "../images/cat.jpeg"# Getting the base64 string
base64_image = encode_image(image_path)headers = {"Content-Type": "application/json","Authorization": f"Bearer " + os.getenv('OPENAI_API_KEY')
}payload = {"model": "gpt-4o","messages": [{"role": "user","content": [{"type": "text","text": "這張照片里有什么?"},{"type": "image_url","image_url": {"url": f"data:image/jpeg;base64,{base64_image}"#input base64 format to url}}]}],"max_tokens": 300
}response = requests.post(os.getenv('OPENAI_BASE_URL') + "/chat/completions", headers=headers, json=payload)print(response.json())
2.4.2 SDK online
from openai import OpenAIclient = OpenAI()
response = client.chat.completions.create(model="gpt-4o",messages=[{"role": "user","content": [{"type": "text", "text": "這張照片里有什么?"},{"type": "image_url","image_url": {"url": "https://p7.itc.cn/q_70/images03/20220805/7a369d8407144b11bfd598091095c959.jpeg",#"url": f"data:image/jpeg;base64,{base64_image}"},},],}],max_tokens=50,
)
print(response.choices[0])
2.5 Respond whit the specified?Json schema
JSON is well-suited for converting data into objects
from openai import OpenAIclient = OpenAI()response = client.chat.completions.create(model="gpt-4o",response_format={"type": "json_object"},#ensure respond is JSON schcemamessages=[{"role": "system", "content": "你是一個助手,請用中文輸出JSON"},{"role": "user", "content": "幫我寫一個冒泡算法?"}]
)
print(response.choices[0].message.content)
before not using JSON schema(more suitable for page display or user reading
?after using JSON schema
2.6 Seed for reproducible output(重現輸出)
As the seed value adcreases,the differences become more pronounced(obvious明顯)
from openai import OpenAI
client = OpenAI()for i in range(3):response = client.chat.completions.create(model="gpt-4o",# 對于三個請求中的每一個,使用相同的 seed 參數 42,同時將所有其他參數保持相同,我們便能夠生成更一致的結果。seed=2, #種子temperature=0.7,max_tokens=50,messages=[{"role": "system", "content": "你是一個生成故事機器人"},{"role": "user", "content": "告訴我一個關于宇宙是如何開始的故事?"}])print(f'故事版本{i + 1}:' + response.choices[0].message.content)del response #this statement is not necessary and can be omitted
seed = 2
?seed = 40
2.7 Count tokens
Count tokens in the development?console for looped chat.
Need to install the tiktoken libary
pip install --upgrade tiktoken
from openai import OpenAI
# pip install --upgrade tiktoken
#tiktoken 用來統計token使用
import tiktoken#package for count tokenclient = OpenAI()
# 初始化 tiktoken 編碼器
encoder = tiktoken.encoding_for_model("gpt-4")def count_tokens(text):encoder.encode(text)# 將輸入的文本text轉換為對應的token列表。具體來說,它使用tiktoken庫中的編碼器將文本進行編碼,以便后續處理。tokens = encoder.encode(text)# 統計文本中的 token 數量return len(tokens)def main():# 初始化聊天記錄messages = [{"role": "system", "content": "You are a helpful assistant."}]print("開始聊天吧!輸入 'exit' 退出。")total_tokens = 0while True:# 獲取用戶輸入user_input = input("用戶: ")if user_input.lower() == 'exit':break# 添加用戶消息到聊天記錄messages.append({"role": "user", "content": user_input})# 統計用戶輸入的 token 數量并累加user_tokens = count_tokens(user_input)total_tokens += user_tokens# 調用 GPT-4 模型response = client.chat.completions.create(model="gpt-4",messages=messages,max_tokens=150,temperature=0.7,top_p=1,n=1)# 獲取助手的回復assistant_message = response.choices[0].message.content.strip()# 添加助手的回復到聊天記錄messages.append({"role": "assistant", "content": assistant_message})# 統計助手回復的 token 數量并累加assistant_tokens = count_tokens(assistant_message)total_tokens += assistant_tokens# 輸出用戶輸入和助手的回復print(f"助手: {assistant_message}")# 輸出當前聊天記錄的總 token 數量print(f"用戶tokens數: {user_tokens},助手tokens數: {assistant_tokens},總token數: {total_tokens}")if __name__ == "__main__":#Only when the module is run as the main program directlymain()
2.8 Consol chat loop with session length management based on maximum token limit
from openai import OpenAI
# pip install tiktoken
import tiktokenclient = OpenAI()# 這是 API 請求和響應的總 token 數量限制。對于 GPT-4 模型,這個值通常是 4096。
MAX_TOKENS = 8 # 設置最大 token 數量
# 這是我們預留給模型響應的 token 數量。我們需要在計算對話的最大 token 數量時減去這個值,以確保有足夠的空間來容納模型的響應。
MAX_RESPONSE_TOKENS = 6 # 設置響應中預留的最大 token 數量
encoder = tiktoken.encoding_for_model("gpt-4")
def count_tokens(text):encoder.encode(text)# 將輸入的文本text轉換為對應的token列表。具體來說,它使用tiktoken庫中的編碼器將文本進行編碼,以便后續處理。tokens = encoder.encode(text)# 統計文本中的 token 數量return len(tokens)
# 假設 MAX_TOKENS 是 4096,而 MAX_RESPONSE_TOKENS 是 500,那么:
# 我們希望對話歷史的 token 數量不要超過 3596 (4096 - 500)。
# 這樣,當我們發送對話歷史給 API 時,仍然有 500 個 token 的空間用于模型生成的響應。
def manage_token_limit(messages):current_tokens = count_tokens(messages)if current_tokens > (MAX_TOKENS - MAX_RESPONSE_TOKENS):print(f"當前會話 token 數量: {current_tokens}, 超過最大 token 數量: {MAX_TOKENS - MAX_RESPONSE_TOKENS}")return Falsereturn Truedef get_gpt_response(messages):"""獲取 GPT-4 的響應"""response = client.chat.completions.create(model="gpt-4",messages=messages)return response.choices[0].message.content.strip()def main():print("Chat with GPT-4. Type 'exit' to end the conversation.")while True:messages = []#this statement must be inside the while loop to ensure that the variable is reset on each iteration(每次迭代)user_input = input("用戶: ")if user_input.lower() == 'exit':breakmessages.append({"role": "user", "content": user_input})# 管理用戶輸入以確保總 token 數量不超過限制if not manage_token_limit(user_input):continueresponse = get_gpt_response(messages)print(f"GPT: {response}")#The role of 'f' is to format string for input variablesmessages.append({"role": "assistant", "content": response})if __name__ == "__main__":main()