From 64358a698acdb016b20f0df9076f243446f2f9b7 Mon Sep 17 00:00:00 2001 From: "K.Y" Date: Thu, 16 May 2024 00:44:48 +0800 Subject: [PATCH] chore: Minor updates --- .github/workflows/docs.yml | 49 ---------------------- Examples/tool_calls.py | 84 ++++++++++++++++++++++++++++++++++++++ README.md | 4 +- README_EN.md | 4 +- 4 files changed, 86 insertions(+), 55 deletions(-) delete mode 100644 .github/workflows/docs.yml create mode 100644 Examples/tool_calls.py diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml deleted file mode 100644 index 417d0c4..0000000 --- a/.github/workflows/docs.yml +++ /dev/null @@ -1,49 +0,0 @@ -name: website - -# build the documentation whenever there are new commits on main -on: - push: - branches: - - main - # Alternative: only build for tags. - # tags: - # - '*' - -# security: restrict permissions for CI jobs. -permissions: - contents: read - -jobs: - # Build the documentation and upload the static HTML files as an artifact. - build: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - - uses: actions/setup-python@v5 - with: - python-version: '3.10' - - # ADJUST THIS: install all dependencies (including pdoc) - - run: pip install -e . - # ADJUST THIS: build your documentation into docs/. - # We use a custom build script for pdoc itself, ideally you just run `pdoc -o docs/ ...` here. - - run: python docs/make.py - - - uses: actions/upload-pages-artifact@v3 - with: - path: docs/ - - # Deploy the artifact to GitHub pages. - # This is a separate job so that only actions/deploy-pages has the necessary permissions. - deploy: - needs: build - runs-on: ubuntu-latest - permissions: - pages: write - id-token: write - environment: - name: github-pages - url: ${{ steps.deployment.outputs.page_url }} - steps: - - id: deployment - uses: actions/deploy-pages@v4 \ No newline at end of file diff --git a/Examples/tool_calls.py b/Examples/tool_calls.py new file mode 100644 index 0000000..673c3af --- /dev/null +++ b/Examples/tool_calls.py @@ -0,0 +1,84 @@ +from openai import OpenAI +from rich import print +from sparrow import MeasureTime, yaml_load # pip install sparrow-python + +config = yaml_load("config.yaml", rel_path=True) +print(f"{config=}") + +client = OpenAI( + api_key=config['api_key'], + base_url=config['api_base'], +) +stream = True + +n = 1 + +# debug = True +debug = False + +caching = True + +max_tokens = None + +model = "gpt-3.5-turbo" +# model="gpt-4" + +mt = MeasureTime().start() +tools = [ + { + "type": "function", + "function": { + "name": "get_current_weather", + "description": "Get the current weather in a given location", + "parameters": { + "type": "object", + "properties": { + "location": { + "type": "string", + "description": "The city and state, e.g. San Francisco, CA", + }, + "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}, + }, + "required": ["location"], + }, + }, + } +] +resp = client.chat.completions.create( + model=model, + messages=[{"role": "user", "content": "What's the weather like in Boston today?"}], + tools=tools, + tool_choice="auto", # auto is default, but we'll be explicit + stream=stream, + extra_body={"caching": caching}, +) + +if stream: + if debug: + for chunk in resp: + print(chunk) + else: + for idx, chunk in enumerate(resp): + chunk_message = chunk.choices[0].delta or "" + if idx == 0: + mt.show_interval("tcp time:") + function = chunk_message.tool_calls[0].function + name = function.name + print(f"{chunk_message.role}: \n{name}: ") + continue + + content = "" + tool_calls = chunk_message.tool_calls + if tool_calls: + function = tool_calls[0].function + if function: + content = function.arguments or "" + print(content, end="") + print() +else: + print(resp) + assistant_content = resp.choices[0].message.content + print(assistant_content) + print(resp.usage) + +mt.show_interval("tool_calls") diff --git a/README.md b/README.md index 0ae1127..68f722d 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ **简体中文** | [**English**](https://github.com/KenyonY/openai-forward/blob/main/README_EN.md)

- 🌠 OpenAI Forward + OpenAI Forward

@@ -26,8 +26,6 @@
-[![Deploy to Render](https://render.com/images/deploy-to-render-button.svg)](https://render.com/deploy?repo=https://github.com/KenyonY/openai-forward) - [特点](#主要特性) | [部署指南](deploy.md) | [使用指南](#使用指南) | diff --git a/README_EN.md b/README_EN.md index 3ae708c..11ca3be 100644 --- a/README_EN.md +++ b/README_EN.md @@ -1,7 +1,7 @@ **English** | [**简体中文**](https://github.com/KenyonY/openai-forward/blob/main/README.md)

- 🌠 OpenAI Forward + OpenAI Forward

@@ -27,8 +27,6 @@
-[![Deploy to Render](https://render.com/images/deploy-to-render-button.svg)](https://render.com/deploy?repo=https://github.com/KenyonY/openai-forward) - [Features](#Key-Features) | [Deployment Guide](deploy_en.md) |