Skip to content

Commit

Permalink
Merge pull request #8 from RexWzh/chattype
Browse files Browse the repository at this point in the history
Use `Chat` class
  • Loading branch information
RexWzh committed Mar 25, 2023
2 parents 51f0878 + ed1542d commit 0947317
Show file tree
Hide file tree
Showing 10 changed files with 438 additions and 166 deletions.
40 changes: 22 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ A simple wrapper for OpenAI API, which can send prompt message and return respon
## Installation

```bash
pip install openai-api-call
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
Expand All @@ -36,15 +38,15 @@ export OPENAI_API_KEY="sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
### Set Proxy (Optional)

```py
from openai_api_call import proxy_on, proxy_off, show_proxy
from openai_api_call import proxy_on, proxy_off, proxy_status
# Check the current proxy
show_proxy()
proxy_status()

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

# Check the updated proxy
show_proxy()
proxy_status()

# Turn off proxy
proxy_off()
Expand All @@ -55,18 +57,17 @@ proxy_off()
Example 1, send prompt and return information:

```python
from openai_api_call import prompt2response, show_apikey
from openai_api_call import Chat, show_apikey

# Check if API key is set
show_apikey()

# Check if proxy is enabled
show_proxy()
proxy_status()

# Send prompt and return response
prompt = "Hello, GPT-3.5!"
resp = prompt2response(prompt)
print(resp.content)
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:
Expand All @@ -79,9 +80,9 @@ openai_api_call.default_prompt = lambda msg: [
{"role": "system", "content": "帮我翻译这段文字"},
{"role": "user", "content": msg}
]
prompt = "Hello!"
chat = Chat("Hello!")
# Set the number of retries to Inf
response = prompt2response(prompt, temperature=0.5, max_requests=-1)
response = chat.getresponse(temperature=0.5, max_requests=-1)
print("Number of consumed tokens: ", response.total_tokens)
print("Returned content: ", response.content)
```
Expand All @@ -92,18 +93,21 @@ Continue chatting based on the last response:

```python
# first call
prompt = "Hello, GPT-3.5!"
resp = prompt2response(prompt)
chat = Chat("Hello, GPT-3.5!")
resp = chat.getresponse() # update chat history, default is True
print(resp.content)

# next call
next_prompt = resp.next_prompt("How are you?")
print(next_prompt)
next_resp = prompt2response(next_prompt)
# 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
list(map(print,next_resp.chat_log()))
chat.print_log()
```

## License
Expand Down
44 changes: 24 additions & 20 deletions README_zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ OpenAI API 的简单封装,用于发送 prompt message 并返回 response。
## 安装方法

```bash
pip install openai-api-call
pip install openai-api-call --upgrade
```

> 注:版本 `0.2.0` 起改用 `Chat` 类型来处理数据,不兼容之前的版本。
## 使用方法

### 设置 API 密钥
Expand All @@ -34,15 +36,15 @@ export OPENAI_API_KEY="sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
### 设置代理(可选)

```py
from openai_api_call import proxy_on, proxy_off, show_proxy
from openai_api_call import proxy_on, proxy_off, proxy_status
# 查看当前代理
show_proxy()
proxy_status()

# 设置本地代理,端口号默认为 7890
proxy_on("127.0.0.1", port=7890)

# 查看更新后的代理
show_proxy()
proxy_status()

# 关闭代理
proxy_off()
Expand All @@ -52,17 +54,17 @@ proxy_off()

示例一,发送 prompt 并返回信息:
```python
from openai_api_call import prompt2response, show_apikey
from openai_api_call import Chat, show_apikey, proxy_status

# 检查 API 密钥是否设置
show_apikey()

# 查看是否开启代理
show_proxy()
proxy_status()

# 发送 prompt 并返回 response
prompt = "Hello, GPT-3.5!"
resp = prompt2response(prompt)
chat = Chat("Hello, GPT-3.5!")
resp = chat.getresponse(update=False) # 不更新对话历史,默认为 True
print(resp.content)
```

Expand All @@ -77,30 +79,32 @@ openai_api_call.default_prompt = lambda msg: [
{"role": "system", "content": "帮我翻译这段文字"},
{"role": "user", "content": msg}
]
prompt = "Hello!"
chat = Chat("Hello!")
# 设置重试次数为 Inf
response = prompt2response(prompt, temperature=0.5, max_requests=-1)
response = chat.getresponse(temperature=0.5, max_requests=-1)
print("Number of consumed tokens: ", response.total_tokens)
print("Returned content: ", response.content)
```

### 进阶用法
继续上一次的对话:
示例三,多轮对话:

```python
# 第一次调用
prompt = "Hello, GPT-3.5!"
resp = prompt2response(prompt)
# 初次对话
chat = Chat("Hello, GPT-3.5!")
resp = chat.getresponse() # 更新对话历史,默认为 True
print(resp.content)

# 下一次调用
next_prompt = resp.next_prompt("How are you?")
print(next_prompt)
next_resp = prompt2response(next_prompt)
# 继续对话
chat.user("How are you?")
next_resp = chat.getresponse()
print(next_resp.content)

# 假装对话历史
chat.user("What's your name?")
chat.assistant("My name is GPT-3.5.")

# 打印对话历史
list(map(print,next_resp.chat_log()))
chat.print_log()
```

## 开源协议
Expand Down
94 changes: 8 additions & 86 deletions openai_api_call/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

__author__ = """Rex Wang"""
__email__ = '1073853456@qq.com'
__version__ = '0.1.0'
__version__ = '0.2.0'

import openai
from typing import List, Dict, Union
import os
from .response import Resp
from .chattool import Chat, Resp
from .proxy import proxy_on, proxy_off, proxy_status

# read API key from the environment variable
if os.environ.get('OPENAI_API_KEY') is not None:
Expand All @@ -16,54 +16,11 @@
print("Warning: The default environment variable `OPENAI_API_KEY` is not a valid API key.")
openai.api_key = apikey

def prompt2response( msg:Union[str, List[Dict]]
, max_requests:int=1
, model:str = "gpt-3.5-turbo"
, strip:bool=True
, **options)->Resp:
"""Transform prompt to the API response
Args:
msg (Union[str, List[Dict]]): prompt message
max_requests (int, optional): maximum number of requests to make. Defaults to 1.
model (str, optional): model to use. Defaults to "gpt-3.5-turbo".
strip (bool, optional): whether to strip the prompt message. Defaults to True.
**options : options for the API call.
Returns:
Resp: API response
"""
# assert max_requests >= 0, "max_requests should be non-negative"
assert openai.api_key is not None, "API key is not set!"

# initialize prompt message
if isinstance(msg, str):
msg = default_prompt(msg)

# default options
if not len(options):
options = {}
# make request
resp = Resp()
while max_requests:
numoftries = 0
try:
resp.response = openai.ChatCompletion.create(
messages=msg, model=model,
**options)
if strip:
resp._strip_content()
resp._request_msg = msg
except:
max_requests -= 1
numoftries += 1
print(f"API call failed! Try again ({numoftries})")
continue
break
def show_apikey():
if openai.api_key is not None:
print(f"API key:\t{openai.api_key}")
else:
raise Exception("API call failed!\nYou can try to update the API key"
+ ", increase `max_requests` or set proxy.")
return resp
print("API key is not set!")

def default_prompt(msg:str):
"""Default prompt message for the API call
Expand All @@ -75,41 +32,6 @@ def default_prompt(msg:str):
List[Dict]: default prompt message
"""
return [{"role": "user", "content": msg},]

def proxy_on(host:str, port:int=7890):
"""Set proxy for the API call
Args:
host (str): proxy host
port (int, optional): proxy port. Defaults to 7890.
"""
host = host.replace("http://", "").replace("https://", "")
os.environ['http_proxy'] = f"http://{host}:{port}"
os.environ['https_proxy'] = f"https://{host}:{port}"

def proxy_off():
"""Turn off proxy for the API call"""
if os.environ.get('http_proxy') is not None:
os.environ.pop('http_proxy')
if os.environ.get('https_proxy') is not None:
os.environ.pop('https_proxy')

def show_proxy():
http = os.environ.get('http_proxy')
https = os.environ.get('https_proxy')
if http is None:
print("`http_proxy` is not set!")
else:
print(f"http_proxy:\t{http}")
if https is None:
print("`https_proxy` is not set!")
else:
print(f"https_proxy:\t{https}")

def show_apikey():
if openai.api_key is not None:
print(f"API key:\t{openai.api_key}")
else:
print("API key is not set!")



0 comments on commit 0947317

Please sign in to comment.