Skip to content

Commit bd2dbcd

Browse files
author
Brandon Meyerowitz
authored
feat: make prompt required for all completion requests (#4)
1 parent 00d9513 commit bd2dbcd

File tree

3 files changed

+31
-12
lines changed

3 files changed

+31
-12
lines changed

commonbase/completion.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def _get_sdk_version() -> str:
2020

2121
def _format_body(
2222
project_id: str,
23-
prompt: Optional[str] = None,
23+
prompt: str,
2424
variables: Optional[dict[str, str]] = None,
2525
chat_context: Optional[ChatContext] = None,
2626
user_id: Optional[str] = None,
@@ -48,7 +48,7 @@ def _format_body(
4848
def _send_completion_request(
4949
api_key: str,
5050
project_id: str,
51-
prompt: Optional[str],
51+
prompt: str,
5252
variables: Optional[dict[str, str]],
5353
chat_context: Optional[ChatContext],
5454
user_id: Optional[str],
@@ -99,7 +99,7 @@ def create(
9999
cls,
100100
api_key: str,
101101
project_id: str,
102-
prompt: Optional[str] = None,
102+
prompt: str,
103103
variables: Optional[dict[str, str]] = None,
104104
chat_context: Optional[ChatContext] = None,
105105
user_id: Optional[str] = None,
@@ -115,7 +115,7 @@ def create(
115115
The Commonbase API key used to authenticate the request.
116116
project_id : str
117117
The ID of the Commonbase project.
118-
prompt : str, optional
118+
prompt : str
119119
The prompt for which a completion is generated.
120120
variables : dict[str, str], optional
121121
The list of variables to use with Commonbase managed prompts.
@@ -163,7 +163,7 @@ def stream(
163163
cls,
164164
api_key: str,
165165
project_id: str,
166-
prompt: Optional[str] = None,
166+
prompt: str,
167167
variables: Optional[dict[str, str]] = None,
168168
chat_context: Optional[ChatContext] = None,
169169
user_id: Optional[str] = None,

commonbase/tests/test_completion.py

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,51 @@
88

99
def test_create_no_api_key():
1010
with pytest.raises(CommonbaseException):
11-
Completion.create(api_key=None, project_id="")
11+
Completion.create(
12+
api_key=None, project_id=os.getenv("CB_PROJECT_ID") or "", prompt=""
13+
)
1214

1315

1416
def test_create_no_project_id():
1517
with pytest.raises(CommonbaseApiException):
16-
Completion.create(api_key="", project_id=None) # type: ignore
18+
Completion.create(api_key=os.getenv("CB_API_KEY") or "", project_id=None, prompt="") # type: ignore
19+
20+
21+
def test_create_no_prompt():
22+
with pytest.raises(CommonbaseApiException):
23+
Completion.create(api_key=os.getenv("CB_API_KEY") or "", project_id=os.getenv("CB_PROJECT_ID") or "", prompt=None) # type: ignore
1724

1825

1926
def test_stream_no_api_key():
2027
with pytest.raises(CommonbaseException):
21-
for _ in Completion.stream(api_key=None, project_id=""):
28+
for _ in Completion.stream(api_key=None, project_id=os.getenv("CB_PROJECT_ID") or "", prompt=""): # type: ignore
2229
pass
2330

2431

2532
def test_stream_no_project_id():
2633
with pytest.raises(CommonbaseApiException):
27-
for _ in Completion.stream(api_key="", project_id=None, prompt=""): # type: ignore
34+
for _ in Completion.stream(api_key=os.getenv("CB_API_KEY") or "", project_id=None, prompt=""): # type: ignore
35+
pass
36+
37+
38+
def test_stream_no_prompt():
39+
with pytest.raises(CommonbaseApiException):
40+
for _ in Completion.stream(api_key=os.getenv("CB_API_KEY") or "", project_id=os.getenv("CB_PROJECT_ID") or "", prompt=None): # type: ignore
2841
pass
2942

3043

3144
def test_create_invalid_project_id():
3245
with pytest.raises(CommonbaseApiException):
33-
Completion.create(api_key="", project_id="", prompt="Hello")
46+
Completion.create(
47+
api_key=os.getenv("CB_API_KEY") or "", project_id="", prompt="Hello"
48+
)
3449

3550

3651
def test_stream_invalid_project_id():
3752
with pytest.raises(CommonbaseApiException):
38-
for _ in Completion.stream(api_key="", project_id="", prompt="Hello"):
53+
for _ in Completion.stream(
54+
api_key=os.getenv("CB_API_KEY") or "", project_id="", prompt="Hello"
55+
):
3956
pass
4057

4158

@@ -74,6 +91,7 @@ def test_completion_variables():
7491
result = Completion.create(
7592
api_key=os.getenv("CB_API_KEY") or "",
7693
project_id=os.getenv("CB_PROJECT_ID") or "",
94+
prompt="My name is {{user_name}} and my email is {{email}}",
7795
variables={"user_name": "USERNAME", "email": "USER@COMPANY.COM"},
7896
)
7997

examples/completion.ipynb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@
104104
"\n",
105105
"result = commonbase.Completion.create(\n",
106106
" api_key=\"API_KEY\",\n",
107-
" project_id=\"PROJECT_ID\", \n",
107+
" project_id=\"PROJECT_ID\",\n",
108+
" prompt=\"A new user {{user_name}} just signed up with {{email}}. Say hello!\", \n",
108109
" variables={\n",
109110
" \"user_name\": \"USERNAME\",\n",
110111
" \"email\": \"USERNAME@COMPANY.COM\"\n",

0 commit comments

Comments
 (0)