Skip to main content

A short wrapper of the OpenAI api call.

Project description

以下为 ChatGPT 自动生成的 README.md 文件(待修改),中文文档移步这里

Openai API call

PyPI version Build Status Documentation Status Updates

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

Installation

pip install git+https://github.com/RexWzh/openai_api_call.git

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

  • TODO

Credits

This package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.

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.1.0.tar.gz (5.6 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.1.0-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: openai_api_call-0.1.0.tar.gz
  • Upload date:
  • Size: 5.6 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.1.0.tar.gz
Algorithm Hash digest
SHA256 1b03856d897b4832290342c84b77959eb3e30dcffd5eb3c30f5964326384022b
MD5 9746568675ff99a868f82e67e2214b71
BLAKE2b-256 c36f5d82a5ca55403ed450889533e7589f8b0fbb4b7795bc9ba784d42f6a67b3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for openai_api_call-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 212a784c6fbd7b8b527604220e410d9ebf27ece2003334fb45f14fb23f4c2e4d
MD5 1f6638fe49b247cab2fb2f83b6b6dfe6
BLAKE2b-256 83435e2fc2e78b010cb6e0d6417523de2fea82eab1635a2a9119f5ecf4d2ae0e

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