Skip to content

Commit

Permalink
Merge pull request #85 from cubenlp/rex/use-env-key
Browse files Browse the repository at this point in the history
use env key in curl
  • Loading branch information
RexWzh authored Jun 24, 2024
2 parents 950937d + 54915bc commit c0a234f
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 7 deletions.
2 changes: 1 addition & 1 deletion chattool/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

__author__ = """Rex Wang"""
__email__ = '[email protected]'
__version__ = '3.3.2'
__version__ = '3.3.3'

import os, sys, requests, json
from .chattype import Chat, Resp
Expand Down
18 changes: 15 additions & 3 deletions chattool/chattype.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,14 +398,26 @@ def get_valid_models(self, gpt_only:bool=True)->List[str]:
model_url = os.path.join(self.base_url, 'v1/models')
return valid_models(self.api_key, model_url, gpt_only=gpt_only)

def get_curl(self, **options):
"""Print the curl command"""
def get_curl(self, use_env_key:bool=False, **options):
"""Get the curl command
Args:
use_env_key (bool, optional): whether to replace the API key with environment variable. Defaults to False.
Returns:
str: curl command
"""
options = self._init_options(**options)
api_key, chat_log, chat_url = self.api_key, self.chat_log, self.chat_url
if use_env_key: api_key = '$OPENAI_API_KEY'
return curl_cmd_of_chat_completion(api_key, chat_url, chat_log, **options)

def print_curl(self, **options):
"""Print the curl command"""
"""Print the curl command
Args:
use_env_key (bool, optional): whether to use the environment key. Defaults to False.
"""
print(self.get_curl(**options))

# Part5: properties and setters
Expand Down
2 changes: 1 addition & 1 deletion chattool/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def curl_cmd_of_chat_completion( api_key:str
}
curl_cmd += f"\n -H 'Content-Type: application/json' \\"
curl_cmd += f"\n -H 'Authorization: Bearer {api_key}' \\"
curl_cmd += f"\n -d '{json.dumps(payload, indent=4)}' \\"
curl_cmd += f"\n -d '{json.dumps(payload, indent=4, ensure_ascii=False)}' \\"
if isinstance(timeout, int) and timeout > 0:
curl_cmd += f"\n --max-time {timeout} \\"
return curl_cmd.rstrip(" \\")
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
with open('README.md') as readme_file:
readme = readme_file.read()

VERSION = '3.3.2'
VERSION = '3.3.3'

requirements = [
'Click>=7.0', 'requests>=2.20', "responses>=0.23", 'aiohttp>=3.8',
Expand Down
2 changes: 1 addition & 1 deletion tests/test_curlcmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def add(a: int, b: int) -> int:
def test_tool_call():
chat = Chat()
chat.user("find the sum of 784359345 and 345345345")
chat.print_curl()
chat.print_curl(use_env_key=True)
chat.settools([add])
chat.print_curl()
chat.tool_type = 'function_call'
Expand Down

0 comments on commit c0a234f

Please sign in to comment.