A short wrapper of the OpenAI api call.
Project description
中文文档移步这里。
Openai API call
A simple wrapper for OpenAI API, which can send prompt message and return response.
Installation
pip install openai-api-call
Usage
Set API Key
import openai
openai.api_key = "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
Or set OPENAI_API_KEY
in ~/.bashrc
to automatically set it when you start the terminal:
# Add the following code to ~/.bashrc
export OPENAI_API_KEY="sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
Set Proxy (Optional)
from openai_api_call import proxy_on, proxy_off, show_proxy
# Check the current proxy
show_proxy()
# Set local proxy, port number is 7890 by default
proxy_on("127.0.0.1", port=7890)
# Check the updated proxy
show_proxy()
# Turn off proxy
proxy_off()
Basic Usage
Example 1, send prompt and return information:
from openai_api_call import prompt2response, show_apikey
# Check if API key is set
show_apikey()
# Check if proxy is enabled
show_proxy()
# Send prompt and return response
prompt = "Hello, GPT-3.5!"
resp = prompt2response(prompt)
print(resp.content)
Example 2, customize the message template and return the information and the number of consumed tokens:
import openai_api_call
# Customize the sending template
openai_api_call.default_prompt = lambda msg: [
{"role": "system", "content": "帮我翻译这段文字"},
{"role": "user", "content": msg}
]
prompt = "Hello!"
# Set the number of retries to Inf
response = prompt2response(prompt, temperature=0.5, max_requests=-1)
print("Number of consumed tokens: ", response.total_tokens)
print("Returned content: ", response.content)
Advanced Usage
Continue chatting based on the last response:
# first call
prompt = "Hello, GPT-3.5!"
resp = prompt2response(prompt)
print(resp.content)
# next call
next_prompt = resp.next_prompt("How are you?")
print(next_prompt)
next_resp = prompt2response(next_prompt)
print(next_resp.content)
# print chat history
list(map(print,next_resp.chat_log()))
License
This package is licensed under the MIT license. See the LICENSE file for more details.
Features
- update documentation of the repo.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
openai_api_call-0.1.1.tar.gz
(5.6 kB
view hashes)
Built Distribution
Close
Hashes for openai_api_call-0.1.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 03fa529f15d5863ee53679719d7a58511ee15696e041a4e09266c503b3020838 |
|
MD5 | 150fbc2a345ce79fc2f10d3c31321429 |
|
BLAKE2b-256 | 3bcab22e986055c1762e83a46532eb41b2298c9fe172ec14ada1f366fe8aad0b |