opperai 0.0.4__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- opperai-0.0.4/.github/workflows/publish.yml +135 -0
- opperai-0.0.4/.gitignore +13 -0
- opperai-0.0.4/PKG-INFO +45 -0
- opperai-0.0.4/README.md +27 -0
- opperai-0.0.4/pyproject.toml +39 -0
- opperai-0.0.4/pytest.ini +2 -0
- opperai-0.0.4/src/opperai/__init__.py +1 -0
- opperai-0.0.4/src/opperai/client.py +42 -0
- opperai-0.0.4/src/opperai/functions.py +35 -0
- opperai-0.0.4/src/opperai/types/__init__.py +34 -0
- opperai-0.0.4/tests/test_functions.py +32 -0
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
name: Build & Publish
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches:
|
|
6
|
+
- main
|
|
7
|
+
tags:
|
|
8
|
+
- v*
|
|
9
|
+
workflow_dispatch:
|
|
10
|
+
|
|
11
|
+
jobs:
|
|
12
|
+
build:
|
|
13
|
+
runs-on: ubuntu-latest
|
|
14
|
+
steps:
|
|
15
|
+
- name: Code checkout
|
|
16
|
+
uses: actions/checkout@v4
|
|
17
|
+
- name: Set up Python
|
|
18
|
+
uses: actions/setup-python@v5
|
|
19
|
+
with:
|
|
20
|
+
python-version: '3.11'
|
|
21
|
+
cache: pip
|
|
22
|
+
- name: Install dependencies
|
|
23
|
+
run: |
|
|
24
|
+
python -m pip install --upgrade pip
|
|
25
|
+
pip install .[test]
|
|
26
|
+
- name: Run lint
|
|
27
|
+
run: |
|
|
28
|
+
pip install ruff==0.1.11
|
|
29
|
+
ruff format --check .
|
|
30
|
+
- name: Run tests
|
|
31
|
+
run: |
|
|
32
|
+
python -m pytest
|
|
33
|
+
- name: Install pypa/build
|
|
34
|
+
run: >-
|
|
35
|
+
python3 -m
|
|
36
|
+
pip install
|
|
37
|
+
build
|
|
38
|
+
--user
|
|
39
|
+
- name: Build a binary wheel and a source tarball
|
|
40
|
+
run: python3 -m build
|
|
41
|
+
- name: Store the distribution packages
|
|
42
|
+
uses: actions/upload-artifact@v3
|
|
43
|
+
with:
|
|
44
|
+
name: python-package-distributions
|
|
45
|
+
path: dist/
|
|
46
|
+
|
|
47
|
+
publish-test:
|
|
48
|
+
name: Publish SDK to TestPyPI
|
|
49
|
+
runs-on: ubuntu-latest
|
|
50
|
+
needs:
|
|
51
|
+
- build
|
|
52
|
+
environment:
|
|
53
|
+
name: testpypi
|
|
54
|
+
url: https://test.pypi.org/p/opperai
|
|
55
|
+
permissions:
|
|
56
|
+
id-token: write
|
|
57
|
+
steps:
|
|
58
|
+
- name: Download all the dists
|
|
59
|
+
uses: actions/download-artifact@v3
|
|
60
|
+
with:
|
|
61
|
+
name: python-package-distributions
|
|
62
|
+
path: dist/
|
|
63
|
+
|
|
64
|
+
- name: Publish package distributions to PyPI
|
|
65
|
+
uses: pypa/gh-action-pypi-publish@release/v1
|
|
66
|
+
with:
|
|
67
|
+
repository-url: https://test.pypi.org/legacy/
|
|
68
|
+
password: ${{ secrets.TEST_PYPI_API_TOKEN }}
|
|
69
|
+
skip-existing: true
|
|
70
|
+
|
|
71
|
+
publish-to-pypi:
|
|
72
|
+
name: >-
|
|
73
|
+
Publish Python SDK to PyPI
|
|
74
|
+
if: startsWith(github.ref, 'refs/tags/')
|
|
75
|
+
needs:
|
|
76
|
+
- build
|
|
77
|
+
runs-on: ubuntu-latest
|
|
78
|
+
environment:
|
|
79
|
+
name: pypi
|
|
80
|
+
url: https://pypi.org/p/opperai
|
|
81
|
+
permissions:
|
|
82
|
+
id-token: write
|
|
83
|
+
|
|
84
|
+
steps:
|
|
85
|
+
- name: Download all the dists
|
|
86
|
+
uses: actions/download-artifact@v3
|
|
87
|
+
with:
|
|
88
|
+
name: python-package-distributions
|
|
89
|
+
path: dist/
|
|
90
|
+
- name: Publish distribution 📦 to PyPI
|
|
91
|
+
uses: pypa/gh-action-pypi-publish@release/v1
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
github-release:
|
|
95
|
+
name: >-
|
|
96
|
+
GitHub Release
|
|
97
|
+
needs:
|
|
98
|
+
- publish-to-pypi
|
|
99
|
+
runs-on: ubuntu-latest
|
|
100
|
+
|
|
101
|
+
permissions:
|
|
102
|
+
contents: write
|
|
103
|
+
id-token: write
|
|
104
|
+
|
|
105
|
+
steps:
|
|
106
|
+
- name: Download all the dists
|
|
107
|
+
uses: actions/download-artifact@v3
|
|
108
|
+
with:
|
|
109
|
+
name: python-package-distributions
|
|
110
|
+
path: dist/
|
|
111
|
+
- name: Sign the dists with Sigstore
|
|
112
|
+
uses: sigstore/gh-action-sigstore-python@v1.2.3
|
|
113
|
+
with:
|
|
114
|
+
inputs: >-
|
|
115
|
+
./dist/*.tar.gz
|
|
116
|
+
./dist/*.whl
|
|
117
|
+
- name: Create GitHub Release
|
|
118
|
+
env:
|
|
119
|
+
GITHUB_TOKEN: ${{ github.token }}
|
|
120
|
+
run: >-
|
|
121
|
+
gh release create
|
|
122
|
+
'${{ github.ref_name }}'
|
|
123
|
+
--repo '${{ github.repository }}'
|
|
124
|
+
--notes ""
|
|
125
|
+
- name: Upload artifact signatures to GitHub Release
|
|
126
|
+
env:
|
|
127
|
+
GITHUB_TOKEN: ${{ github.token }}
|
|
128
|
+
# Upload to GitHub Release using the `gh` CLI.
|
|
129
|
+
# `dist/` contains the built packages, and the
|
|
130
|
+
# sigstore-produced signatures and certificates.
|
|
131
|
+
run: >-
|
|
132
|
+
gh release upload
|
|
133
|
+
'${{ github.ref_name }}' dist/**
|
|
134
|
+
--repo '${{ github.repository }}'
|
|
135
|
+
|
opperai-0.0.4/.gitignore
ADDED
opperai-0.0.4/PKG-INFO
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: opperai
|
|
3
|
+
Version: 0.0.4
|
|
4
|
+
Summary: Opper Python client
|
|
5
|
+
Project-URL: Homepage, https://opper.ai
|
|
6
|
+
Project-URL: Documentation, https://docs.opper.ai
|
|
7
|
+
Project-URL: Platform, https://platform.opper.ai
|
|
8
|
+
Author-email: Opper <opper@opper.ai>
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Requires-Dist: httpx
|
|
13
|
+
Requires-Dist: pydantic
|
|
14
|
+
Provides-Extra: test
|
|
15
|
+
Requires-Dist: pytest; extra == 'test'
|
|
16
|
+
Requires-Dist: pytest-asyncio; extra == 'test'
|
|
17
|
+
Description-Content-Type: text/markdown
|
|
18
|
+
|
|
19
|
+
# [Opper](https//opper.ai) Python SDK
|
|
20
|
+
|
|
21
|
+
## Install
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
pip install opperai
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Functions
|
|
28
|
+
|
|
29
|
+
To call a function you created at [https://platform.opper.ai](https://platform.opper.ai) you can use the following code:
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
```python
|
|
33
|
+
from opper import Client
|
|
34
|
+
from opper.types import ChatPayload, Message
|
|
35
|
+
|
|
36
|
+
# Use AsyncClient for async operations
|
|
37
|
+
client = Client(api_key="your-api-key")
|
|
38
|
+
response = client.functions.chat("your-function-path",
|
|
39
|
+
ChatPayload(messages=[Message(role="user", content="hello")])
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
print(response)
|
|
43
|
+
|
|
44
|
+
```
|
|
45
|
+
|
opperai-0.0.4/README.md
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# [Opper](https//opper.ai) Python SDK
|
|
2
|
+
|
|
3
|
+
## Install
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
pip install opperai
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
## Functions
|
|
10
|
+
|
|
11
|
+
To call a function you created at [https://platform.opper.ai](https://platform.opper.ai) you can use the following code:
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
```python
|
|
15
|
+
from opper import Client
|
|
16
|
+
from opper.types import ChatPayload, Message
|
|
17
|
+
|
|
18
|
+
# Use AsyncClient for async operations
|
|
19
|
+
client = Client(api_key="your-api-key")
|
|
20
|
+
response = client.functions.chat("your-function-path",
|
|
21
|
+
ChatPayload(messages=[Message(role="user", content="hello")])
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
print(response)
|
|
25
|
+
|
|
26
|
+
```
|
|
27
|
+
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "opperai"
|
|
7
|
+
version = "0.0.4"
|
|
8
|
+
description = "Opper Python client"
|
|
9
|
+
authors = [
|
|
10
|
+
{name = "Opper", email = "opper@opper.ai"},
|
|
11
|
+
]
|
|
12
|
+
|
|
13
|
+
classifiers = [
|
|
14
|
+
"Programming Language :: Python :: 3",
|
|
15
|
+
"License :: OSI Approved :: MIT License",
|
|
16
|
+
"Operating System :: OS Independent",
|
|
17
|
+
]
|
|
18
|
+
dependencies = [
|
|
19
|
+
"pydantic",
|
|
20
|
+
"httpx"
|
|
21
|
+
]
|
|
22
|
+
readme = "README.md"
|
|
23
|
+
|
|
24
|
+
[project.urls]
|
|
25
|
+
Homepage = "https://opper.ai"
|
|
26
|
+
Documentation = "https://docs.opper.ai"
|
|
27
|
+
Platform = "https://platform.opper.ai"
|
|
28
|
+
|
|
29
|
+
[project.optional-dependencies]
|
|
30
|
+
test = [
|
|
31
|
+
"pytest",
|
|
32
|
+
"pytest-asyncio"
|
|
33
|
+
]
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
[tool.pytest.ini_options]
|
|
37
|
+
pythonpath = [
|
|
38
|
+
"src"
|
|
39
|
+
]
|
opperai-0.0.4/pytest.ini
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from .client import AsyncClient, Client
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import httpx
|
|
2
|
+
from .functions import Functions, AsyncFunctions
|
|
3
|
+
|
|
4
|
+
DEFAULT_API_URL = "https://api.opper.ai"
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class AsyncClient:
|
|
8
|
+
def __init__(self, api_key: str, api_url: str = DEFAULT_API_URL):
|
|
9
|
+
self.http_client = _async_http_client(api_key, api_url)
|
|
10
|
+
self.functions = AsyncFunctions(self.http_client)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class Client:
|
|
14
|
+
def __init__(self, api_key: str, api_url: str = DEFAULT_API_URL):
|
|
15
|
+
self.http_client = _http_client(api_key, api_url)
|
|
16
|
+
self.functions = Functions(self.http_client)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class _async_http_client:
|
|
20
|
+
def __init__(self, api_key: str, api_url):
|
|
21
|
+
self.session = httpx.AsyncClient(
|
|
22
|
+
base_url=api_url, headers={"X-OPPER-API-KEY": f"{api_key}"}
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
async def do_request(self, method: str, path: str, **kwargs):
|
|
26
|
+
response = await self.session.request(method, path, **kwargs)
|
|
27
|
+
if response.status_code != 200:
|
|
28
|
+
raise Exception(f"Request failed with status {response.status_code}")
|
|
29
|
+
return response.json()
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class _http_client:
|
|
33
|
+
def __init__(self, api_key: str, api_url: str):
|
|
34
|
+
self.session = httpx.Client(
|
|
35
|
+
base_url=api_url, headers={"X-OPPER-API-KEY": f"{api_key}"}
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
def do_request(self, method: str, path: str, **kwargs):
|
|
39
|
+
response = self.session.request(method, path, **kwargs)
|
|
40
|
+
if response.status_code != 200:
|
|
41
|
+
raise Exception(f"Request failed with status {response.status_code}")
|
|
42
|
+
return response.json()
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
from .types import ChatPayload, FunctionResponse
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class Functions:
|
|
5
|
+
def __init__(self, http_client):
|
|
6
|
+
self.http_client = http_client
|
|
7
|
+
|
|
8
|
+
def chat(self, function_path, data: ChatPayload, stream=False) -> FunctionResponse:
|
|
9
|
+
serialized_data = data.model_dump()
|
|
10
|
+
|
|
11
|
+
data = self.http_client.do_request(
|
|
12
|
+
"POST",
|
|
13
|
+
f"/v1/chat/{function_path}",
|
|
14
|
+
json=serialized_data,
|
|
15
|
+
params={"stream": "true"} if stream else None,
|
|
16
|
+
)
|
|
17
|
+
return FunctionResponse(**data)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class AsyncFunctions:
|
|
21
|
+
def __init__(self, http_client):
|
|
22
|
+
self.http_client = http_client
|
|
23
|
+
|
|
24
|
+
async def chat(
|
|
25
|
+
self, function_path, data: ChatPayload, stream=False
|
|
26
|
+
) -> FunctionResponse:
|
|
27
|
+
serialized_data = data.model_dump()
|
|
28
|
+
|
|
29
|
+
data = await self.http_client.do_request(
|
|
30
|
+
"POST",
|
|
31
|
+
f"/v1/chat/{function_path}",
|
|
32
|
+
json=serialized_data,
|
|
33
|
+
params={"stream": "true"} if stream else None,
|
|
34
|
+
)
|
|
35
|
+
return FunctionResponse(**data)
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
from pydantic import BaseModel, Field
|
|
2
|
+
from typing import List, Optional
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class Message(BaseModel):
|
|
6
|
+
role: str
|
|
7
|
+
content: str
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class ChatPayload(BaseModel):
|
|
11
|
+
messages: List[Message]
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class ContextData(BaseModel):
|
|
15
|
+
embeddings_id: str
|
|
16
|
+
embeddings_table: str
|
|
17
|
+
dataset_id: int
|
|
18
|
+
content: str
|
|
19
|
+
score: Optional[float]
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class DebugData(BaseModel):
|
|
23
|
+
context: List[ContextData]
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class StreamingChunk(BaseModel):
|
|
27
|
+
delta: str
|
|
28
|
+
error: Optional[str] = None
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class FunctionResponse(BaseModel):
|
|
32
|
+
message: str
|
|
33
|
+
error: Optional[str] = None
|
|
34
|
+
debug: Optional[DebugData] = None
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import pytest
|
|
2
|
+
from opperai import AsyncClient, Client
|
|
3
|
+
from opperai.types import ChatPayload, Message
|
|
4
|
+
|
|
5
|
+
from unittest.mock import patch
|
|
6
|
+
|
|
7
|
+
api_key = "key"
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
@patch("opperai.client._http_client.do_request")
|
|
11
|
+
def test_chat(mock_do_request):
|
|
12
|
+
mock_do_request.return_value = {"message": "Bonjour"}
|
|
13
|
+
client = Client(api_key=api_key)
|
|
14
|
+
resp = client.functions.chat(
|
|
15
|
+
"french", ChatPayload(messages=[Message(role="user", content="hello")])
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
assert resp.message == "Bonjour"
|
|
19
|
+
mock_do_request.assert_called_once()
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
@pytest.mark.asyncio
|
|
23
|
+
@patch("opperai.client._async_http_client.do_request")
|
|
24
|
+
async def test_async_chat(mock_do_request):
|
|
25
|
+
mock_do_request.return_value = {"message": "Bonjour"}
|
|
26
|
+
client = AsyncClient(api_key="op-dev-api-key", api_url="http://localhost:8000")
|
|
27
|
+
resp = await client.functions.chat(
|
|
28
|
+
"french", ChatPayload(messages=[Message(role="user", content="hello")])
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
assert resp.message == "Bonjour"
|
|
32
|
+
mock_do_request.assert_called_once()
|