Skip to main content

A short wrapper of the OpenAI api call.

Project description

中文文档移步这里

Openai API call

PyPI version Tests Documentation Status

A simple wrapper for OpenAI API, which can send prompt message and return response.

Installation

pip install openai-api-call --upgrade

Note: Since version 0.2.0, Chat type is used to handle data, which is not compatible with previous versions.

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, proxy_status
# Check the current proxy
proxy_status()

# Set local proxy, port number is 7890 by default
proxy_on("127.0.0.1", port=7890)

# Check the updated proxy
proxy_status()

# Turn off proxy
proxy_off() 

Basic Usage

Example 1, send prompt and return information:

from openai_api_call import Chat, show_apikey

# Check if API key is set
show_apikey()

# Check if proxy is enabled
proxy_status()

# Send prompt and return response
chat = Chat("Hello, GPT-3.5!")
resp = chat.getresponse(update=False) # Do not update the chat history, default is True

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}
]
chat = Chat("Hello!")
# Set the number of retries to Inf
response = chat.getresponse(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
chat = Chat("Hello, GPT-3.5!")
resp = chat.getresponse() # update chat history, default is True
print(resp.content)

# continue chatting
chat.user("How are you?")
next_resp = chat.getresponse()
print(next_resp.content)

# fake response
chat.user("What's your name?")
chat.assistant("My name is GPT-3.5.")

# print chat history
chat.print_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


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.2.0.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

openai_api_call-0.2.0-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

Details for the file openai_api_call-0.2.0.tar.gz.

File metadata

  • Download URL: openai_api_call-0.2.0.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.13

File hashes

Hashes for openai_api_call-0.2.0.tar.gz
Algorithm Hash digest
SHA256 3c1ed030dba1bfb02d7077197636e1ee52f1b2f18ca7ba2e2ca4c7e74450663a
MD5 447f3cf14921ad2739026c7cf96f80f3
BLAKE2b-256 9e4c68dd3086b69de08ba47534ab76a20317f9a07df29e9c7e7e6288ccc24fe2

See more details on using hashes here.

File details

Details for the file openai_api_call-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for openai_api_call-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d2d1e44d86fc813841d871d84ee58d967d8c902a76f6ec8dc3413a7d9cdf511f
MD5 5a74f054df65377cf710330a7798598f
BLAKE2b-256 f22d4377d9b594acdd3d0b64585b13c1a2ff1f2d82d14b331806259ecab00cb8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page