Skip to main content

A short wrapper of the OpenAI api call.

Project description

中文文档移步这里

Openai API call

PyPI version Tests Documentation Status Coverage

A Python wrapper for OpenAI API, supporting multi-turn dialogue, proxy, and asynchronous data processing.

Installation

pip install openai-api-call --upgrade

Usage

Set API Key and Base URL

Method 1, write in Python code:

import openai_api_call
openai_api_call.api_key = "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
openai_api_call.base_url = "https://api.example.com"

Method 2, set environment variables in ~/.bashrc or ~/.zshrc:

export OPENAI_API_KEY="sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
export OPENAI_BASE_URL="https://api.example.com"

Examples

Example 1, simulate multi-turn dialogue:

# first chat
chat = Chat("Hello, GPT-3.5!")
resp = chat.getresponse()

# continue the chat
chat.user("How are you?")
next_resp = chat.getresponse()

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

# save the chat history
chat.save("chat.json", mode="w") # default to "a"

# print the chat history
chat.print_log()

Example 2, process data in batch, and use a checkpoint file checkpoint:

# write a function to process the data
def msg2chat(msg):
    chat = Chat(api_key=api_key)
    chat.system("You are a helpful translator for numbers.")
    chat.user(f"Please translate the digit to Roman numerals: {msg}")
    chat.getresponse()

checkpoint = "chat.jsonl"
msgs = ["%d" % i for i in range(1, 10)]
# process the data
chats = process_chats(msgs[:5], msg2chat, checkpoint, clearfile=True)
# process the rest data, and read the cache from the last time
continue_chats = process_chats(msgs, msg2chat, checkpoint)

Example 3, process data in batch (asynchronous), print hello using different languages, and use two coroutines:

from openai_api_call import async_chat_completion

chatlogs = [
    {"role":"user", "content":"print hello using %s" % lang} 
    for lang in ["python", "java", "Julia", "C++"]]
async_chat_completion(chatlogs, chkpoint="async_chat.jsonl", ncoroutines=2)

License

This package is licensed under the MIT license. See the LICENSE file for more details.

update log

Current version 1.0.0 is a stable version, with the redundant feature function call removed, and the asynchronous data processing tool added.

Beta version

  • Since version 0.2.0, Chat type is used to handle data
  • Since version 0.3.0, you can use different API Key to send requests.
  • Since version 0.4.0, this package is mantained by cubenlp.
  • Since version 0.5.0, one can use process_chats to process the data, with a customized msg2chat function and a checkpoint file.
  • Since version 0.6.0, the feature function call is added.

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

Uploaded Source

Built Distribution

openai_api_call-1.1.1-py3-none-any.whl (15.0 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for openai_api_call-1.1.1.tar.gz
Algorithm Hash digest
SHA256 0f582543c4d26ad078f72d8b331c4bc427e24b186e7a56b045dc9d5008a1b4f9
MD5 a29c6d909ab82cdf81a939d23579790f
BLAKE2b-256 1b8351bfae085551009d6f184c04434221d2e953bebcae76e47672d30cf857bb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for openai_api_call-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9f65baf23bb504948b5e0abdc514e24e35fc49136b743242a9a8cf3078585519
MD5 baeebf84652c5e37a8e93a87ca9ca6ce
BLAKE2b-256 de43d96a0621a8ceb1bd93a92ada610b07edb00d463fc64607a10c83a57dff32

See more details on using hashes here.

Supported by

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