Skip to content

Commit

Permalink
fix: modify prompt templating to be more Pythonic (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
viveknair committed Apr 14, 2023
1 parent c4da66f commit 0c96b84
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 16 deletions.
4 changes: 2 additions & 2 deletions examples/examples/openai/create-completion-async.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ async def main():

result = await openai.Completion.acreate(
model="text-davinci-003",
promptTemplate="Hello world {{ name }}",
promptInputs={"name": "Vivek"},
prompt_template="Hello world {{ name }}",
prompt_inputs={"name": "Vivek"},
stream=True,
)

Expand Down
4 changes: 2 additions & 2 deletions examples/examples/openai/create-completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@

result = openai.Completion.create(
model="text-davinci-003",
promptTemplate="Hello world {{ name }}",
promptInputs={"name": "test"},
prompt_template="Hello world {{ name }}",
prompt_inputs={"name": "test"},
)

print("Result: ", result)
Expand Down
4 changes: 2 additions & 2 deletions examples/examples/openai/create-multiple-calls.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@

completion_result = openai.Completion.create(
model="text-davinci-003",
promptTemplate="Hello world {{ name }}",
promptInputs={"name": "test"},
prompt_template="Hello world {{ name }}",
prompt_inputs={"name": "test"},
)

print("Completion result: ", completion_result)
Expand Down
20 changes: 10 additions & 10 deletions package/gentrace/providers/llms/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,22 @@ def create_step_run(
def intercept_completion(original_fn):
@classmethod
def wrapper(cls, *args, **kwargs):
prompt_template = kwargs.get("promptTemplate")
prompt_inputs = kwargs.get("promptInputs")
prompt_template = kwargs.get("prompt_template")
prompt_inputs = kwargs.get("prompt_inputs")
base_completion_options = {
k: v
for k, v in kwargs.items()
if k not in ["promptTemplate", "promptInputs"]
if k not in ["prompt_template", "prompt_inputs"]
}

if "prompt" in base_completion_options:
raise ValueError(
"The prompt attribute cannot be provided when using the Gentrace SDK. Use promptTemplate and promptInputs instead."
"The prompt attribute cannot be provided when using the Gentrace SDK. Use prompt_template and prompt_inputs instead."
)

if not prompt_template:
raise ValueError(
"The promptTemplate attribute must be provided when using the Gentrace SDK."
"The prompt_template attribute must be provided when using the Gentrace SDK."
)

rendered_prompt = pystache.render(prompt_template, prompt_inputs)
Expand All @@ -103,23 +103,23 @@ def wrapper(cls, *args, **kwargs):
def intercept_completion_async(original_fn):
@classmethod
async def wrapper(cls, *args, **kwargs):
prompt_template = kwargs.get("promptTemplate")
prompt_inputs = kwargs.get("promptInputs")
prompt_template = kwargs.get("prompt_template")
prompt_inputs = kwargs.get("prompt_inputs")
stream = kwargs.get("stream")
base_completion_options = {
k: v
for k, v in kwargs.items()
if k not in ["promptTemplate", "promptInputs"]
if k not in ["prompt_template", "prompt_inputs"]
}

if "prompt" in base_completion_options:
raise ValueError(
"The prompt attribute cannot be provided when using the Gentrace SDK. Use promptTemplate and promptInputs instead."
"The prompt attribute cannot be provided when using the Gentrace SDK. Use prompt_template and prompt_inputs instead."
)

if not prompt_template:
raise ValueError(
"The promptTemplate attribute must be provided when using the Gentrace SDK."
"The prompt_template attribute must be provided when using the Gentrace SDK."
)

if stream:
Expand Down
2 changes: 2 additions & 0 deletions package/gentrace/providers/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from datetime import datetime

__all__ = ["to_date_string"]


def to_date_string(time_value):
return (
Expand Down

0 comments on commit 0c96b84

Please sign in to comment.