Skip to content

Commit 7978acd

Browse files
author
Brandon Meyerowitz
authored
feat: add functions api (#6)
1 parent 667aec7 commit 7978acd

16 files changed

Lines changed: 799 additions & 254 deletions

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,3 +164,6 @@ cython_debug/
164164

165165
# VS Code
166166
.vscode/
167+
168+
# direnv
169+
.envrc

README.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
Commonbase allows developers to integrate with any popular LLM API provider
66
without needing to change any code. The SDK helps with collecting data and
7-
feedback from the users and helps you fine-tune models for your specific use case.
7+
feedback from the users and helps you fine-tune models for your specific use
8+
case.
89

910
## Installation
1011

@@ -14,9 +15,12 @@ pip install commonbase
1415

1516
## Usage
1617

17-
A Project ID and API Key are required for all Commonbase requests. You can find your project ID and generate an API key in the [Commonbase Dashboard](https://commonbase.com/).
18+
A Project ID and API Key are required for all Commonbase requests. You can find
19+
your project ID and generate an API key in the
20+
[Commonbase Dashboard](https://commonbase.com/).
1821

19-
To create a completion, provide your Project ID, API Key, and prompt to `Completion.create`.
22+
To create a text completion, provide your Project ID, API Key, and prompt to
23+
`Completion.create`.
2024

2125
```py
2226
import commonbase
@@ -27,9 +31,11 @@ result = commonbase.Completion.create(
2731
prompt="Hello!"
2832
)
2933

30-
print(result.best_result)
34+
print(result.best_choice.text)
3135
```
3236

33-
To stream a completion as it is generated, use `Completion.stream`.
37+
To stream a completion as it is generated, use `ChatCompletion.stream`.
3438

35-
For more examples, see [/examples](https://github.com/commonbaseapp/commonbase-python/tree/main/examples) or check out our [Docs](https://docs.commonbase.com/quickstart/python).
39+
For more examples, see
40+
[/examples](https://github.com/commonbaseapp/commonbase-python/tree/main/examples)
41+
or check out our [Docs](https://docs.commonbase.com/quickstart/python).

commonbase/__init__.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
from commonbase.completion import Completion
2-
from commonbase.exceptions import CommonbaseApiException, CommonbaseException
3-
from commonbase.chat_context import ChatContext, ChatMessage
4-
from commonbase.provider_config import ProviderConfig, OpenAIParams, AnthropicParams
5-
from commonbase.completion_response import CompletionResponse
1+
from commonbase.completion import Completion, ChatCompletion # type: ignore
2+
from commonbase.exceptions import CommonbaseApiException, CommonbaseException # type: ignore
3+
from commonbase.provider_config import OpenAIParams, AnthropicParams # type: ignore
4+
from commonbase.completion_response import CompletionResponse # type: ignore
5+
from commonbase.chat_context import ChatMessage # type: ignore
66

77
__all__: [
88
"Completion",
9+
"ChatCompletion",
10+
"ChatMessage",
911
"CommonbaseException",
1012
"CommonbaseApiException",
11-
"ChatContext",
12-
"ChatMessage",
13-
"ProviderConfig",
1413
"OpenAIParams",
1514
"AnthropicParams",
1615
"CompletionResponse",

commonbase/chat_context.py

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,37 @@
1-
from typing import Literal, Sequence
2-
from dataclasses import dataclass, asdict
1+
from typing import Literal, NotRequired, TypedDict, Optional
32

43

5-
@dataclass
6-
class ChatMessage:
7-
role: Literal["system", "user", "assistant"]
4+
class FunctionCall(TypedDict):
5+
name: str
6+
arguments: Optional[str]
7+
8+
9+
class RegularChatMessage(TypedDict):
10+
role: Literal["system", "user"]
11+
content: str
12+
13+
14+
class AssistantChatMessage(TypedDict):
15+
role: Literal["assistant"]
16+
content: str | None
17+
function_call: NotRequired[FunctionCall]
18+
19+
20+
class FunctionChatMessage(TypedDict):
21+
role: Literal["function"]
22+
name: str
823
content: str
924

1025

11-
@dataclass
12-
class ChatContext:
13-
messages: Sequence[ChatMessage]
26+
class FunctionCallConfigName(TypedDict):
27+
name: str
28+
29+
30+
class ChatFunction(TypedDict):
31+
name: str
32+
description: str
33+
parameters: object
34+
1435

15-
def _as_json(self) -> dict:
16-
return asdict(self)
36+
ChatMessage = RegularChatMessage | AssistantChatMessage | FunctionChatMessage
37+
FunctionCallConfig = Literal["none", "auto"] | FunctionCallConfigName

0 commit comments

Comments
 (0)