Skip to content

Commit

Permalink
fix: allow prompt, change to pipelineRunId (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
viveknair committed Apr 27, 2023
1 parent d21c11f commit 8d449cf
Show file tree
Hide file tree
Showing 22 changed files with 331 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ async def main():

pipeline_run_id = None
async for value in result:
if value.get("pipeline_run_id"):
pipeline_run_id = value.get("pipeline_run_id")
if value.get("pipelineRunId"):
pipeline_run_id = value.get("pipelineRunId")

gentrace.flush()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async def main():

gentrace.flush()

print("Result: ", result.pipeline_run_id)
print("Result: ", result["pipelineRunId"])


asyncio.run(main())
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
pipeline_run_id = None

for value in result:
if value.get("pipeline_run_id"):
pipeline_run_id = value.get("pipeline_run_id")
if value.get("pipelineRunId"):
pipeline_run_id = value.get("pipelineRunId")

print("Result: ", pipeline_run_id)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ async def main():

pipeline_run_id = None
async for value in result:
if value.get("pipeline_run_id"):
pipeline_run_id = value.get("pipeline_run_id")
if value.get("pipelineRunId"):
pipeline_run_id = value.get("pipelineRunId")

gentrace.flush()

Expand Down
2 changes: 1 addition & 1 deletion examples/examples/simple/openai/create-completion-async.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ async def main():

gentrace.flush()

print("Result: ", result.pipeline_run_id)
print("Result: ", result["pipelineRunId"])


asyncio.run(main())
4 changes: 2 additions & 2 deletions examples/examples/simple/openai/create-completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
openai.api_key = os.getenv("OPENAI_KEY")

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

gentrace.flush()

print("Result: ", result.pipeline_run_id)
print("Result: ", result["pipelineRunId"])
2 changes: 1 addition & 1 deletion examples/examples/simple/openai/create-embedding-async.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async def main():

gentrace.flush()

print("Result: ", result.pipeline_run_id)
print("Result: ", result["pipelineRunId"])


asyncio.run(main())
2 changes: 1 addition & 1 deletion examples/examples/simple/openai/create-embedding.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@

gentrace.flush()

print("Result: ", result.pipeline_run_id)
print("Result: ", result["pipelineRunId"])
2 changes: 1 addition & 1 deletion examples/examples/simple/pinecone/delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@

gentrace.flush()

print("Result: ", result.pipeline_run_id)
print("Result: ", result["pipelineRunId"])
2 changes: 1 addition & 1 deletion examples/examples/simple/pinecone/fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@

gentrace.flush()

print("Result: ", result.pipeline_run_id)
print("Result: ", result["pipelineRunId"])
4 changes: 2 additions & 2 deletions examples/examples/simple/pinecone/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
)

result = pinecone.Index("openai-trec").query(
top_k=10, vector=DEFAULT_VECTOR, pipline_id="self-contained-pinecone-query"
top_k=10, vector=DEFAULT_VECTOR, pipeline_id="self-contained-pinecone-query"
)


gentrace.flush()

print("Result: ", result.pipeline_run_id)
print("Result: ", result["pipelineRunId"])
2 changes: 1 addition & 1 deletion examples/examples/simple/pinecone/upsert.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@

gentrace.flush()

print("Result: ", result.pipeline_run_id)
print("Result: ", result["pipelineRunId"])
15 changes: 8 additions & 7 deletions examples/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ repository = "https://github.com/gentrace/gentrace-python"
version = "0.0.1"

[tool.poetry.dependencies]
gentrace-py = {path = "../package/dist/gentrace_py-0.5.1.tar.gz", develop = true}
openai = "^0.27.4"
pinecone-client = "^2.2.1"
python = "^3.11"
python-dotenv = "^1.0.0"
gentrace-py = {path = "../package/dist/gentrace_py-0.5.5.tar.gz", develop = true}

[tool.poetry.group.lint.dependencies]
black = "^23.3.0"
Expand Down
11 changes: 4 additions & 7 deletions package/gentrace/providers/getters.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@
import re

import openai
from urllib3.util import parse_url

from gentrace.configuration import Configuration as GentraceConfiguration

openai.api_key = os.getenv("OPENAI_KEY")


VALID_GENTRACE_HOST = r"^https?://[\w.-]+:\d{1,5}/api/v1/?$"


def test_validity():
from gentrace import api_key, host

if not api_key:
raise ValueError("Gentrace API key not set")

if host and not re.match(VALID_GENTRACE_HOST, host):
path = parse_url(host).path

if host and path != "/api/v1" and path != "/api/v1/":
raise ValueError("Gentrace host is invalid")


Expand All @@ -28,9 +28,6 @@ def configure_openai():

test_validity()

if host and not re.match(VALID_GENTRACE_HOST, host):
raise ValueError("Gentrace host is invalid")

gentrace_config = GentraceConfiguration(host=host)
gentrace_config.access_token = api_key

Expand Down
70 changes: 35 additions & 35 deletions package/gentrace/providers/llms/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def create_completion_step_run(
submit_result = pipeline_run.submit()

if not stream:
completion.pipeline_run_id = (
completion["pipelineRunId"] = (
submit_result["pipelineRunId"]
if "pipelineRunId" in submit_result
else None
Expand Down Expand Up @@ -136,6 +136,7 @@ def intercept_completion(original_fn, gentrace_config: Configuration):
def wrapper(cls, *args, **kwargs):
prompt_template = kwargs.get("prompt_template")
prompt_inputs = kwargs.get("prompt_inputs")
prompt = kwargs.get("prompt")
pipeline_id = kwargs.pop("pipeline_id", None)
stream = kwargs.get("stream")
base_completion_options = {
Expand All @@ -144,18 +145,11 @@ def wrapper(cls, *args, **kwargs):
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 prompt_template and prompt_inputs instead."
)

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

if stream:
rendered_prompt = pystache.render(prompt_template, prompt_inputs)
rendered_prompt = prompt

if prompt_template and prompt_inputs:
rendered_prompt = pystache.render(prompt_template, prompt_inputs)

new_completion_options = {
**base_completion_options,
Expand All @@ -173,7 +167,7 @@ def profiled_completion():
modified_response = []
for value in completion:
if value and is_self_contained:
value["pipeline_run_id"] = pipeline_run_id
value["pipelineRunId"] = pipeline_run_id
modified_response.append(value)
yield value

Expand All @@ -197,9 +191,15 @@ def profiled_completion():

return profiled_completion()

rendered_prompt = pystache.render(prompt_template, prompt_inputs)
rendered_prompt = prompt

if prompt_template and prompt_inputs:
rendered_prompt = pystache.render(prompt_template, prompt_inputs)

new_completion_options = {**base_completion_options, "prompt": rendered_prompt}
new_completion_options = {
**base_completion_options,
"prompt": rendered_prompt,
}

start_time = time.time()
completion = original_fn(**new_completion_options)
Expand Down Expand Up @@ -230,6 +230,7 @@ def intercept_completion_async(original_fn, gentrace_config: Configuration):
async def wrapper(cls, *args, **kwargs):
prompt_template = kwargs.get("prompt_template")
prompt_inputs = kwargs.get("prompt_inputs")
prompt = kwargs.get("prompt")
pipeline_id = kwargs.pop("pipeline_id", None)
stream = kwargs.get("stream")
base_completion_options = {
Expand All @@ -238,18 +239,11 @@ async def wrapper(cls, *args, **kwargs):
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 prompt_template and prompt_inputs instead."
)

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

if stream:
rendered_prompt = pystache.render(prompt_template, prompt_inputs)
rendered_prompt = prompt

if prompt_template and prompt_inputs:
rendered_prompt = pystache.render(prompt_template, prompt_inputs)

new_completion_options = {
**base_completion_options,
Expand All @@ -267,7 +261,7 @@ async def profiled_completion():
modified_response = []
async for value in completion:
if value and is_self_contained:
value["pipeline_run_id"] = pipeline_run_id
value["pipelineRunId"] = pipeline_run_id
modified_response.append(value)
yield value

Expand All @@ -291,9 +285,15 @@ async def profiled_completion():

return profiled_completion()

rendered_prompt = pystache.render(prompt_template, prompt_inputs)
rendered_prompt = prompt

if prompt_template and prompt_inputs:
rendered_prompt = pystache.render(prompt_template, prompt_inputs)

new_completion_options = {**base_completion_options, "prompt": rendered_prompt}
new_completion_options = {
**base_completion_options,
"prompt": rendered_prompt,
}

start_time = time.time()
completion = await original_fn(**new_completion_options)
Expand Down Expand Up @@ -341,7 +341,7 @@ def profiled_completion():
modified_response = []
for value in completion:
if value and is_self_contained:
value["pipeline_run_id"] = pipeline_run_id
value["pipelineRunId"] = pipeline_run_id
modified_response.append(value)
yield value

Expand Down Expand Up @@ -416,7 +416,7 @@ def profiled_completion():

if is_self_contained:
submit_result = pipeline_run.submit()
completion.pipeline_run_id = (
completion["pipelineRunId"] = (
submit_result["pipelineRunId"]
if "pipelineRunId" in submit_result
else None
Expand Down Expand Up @@ -450,7 +450,7 @@ async def profiled_completion():
modified_response = []
async for value in completion:
if value and is_self_contained:
value["pipeline_run_id"] = pipeline_run_id
value["pipelineRunId"] = pipeline_run_id
modified_response.append(value)
yield value

Expand Down Expand Up @@ -526,7 +526,7 @@ async def profiled_completion():

if is_self_contained:
submit_result = pipeline_run.submit()
completion.pipeline_run_id = (
completion["pipelineRunId"] = (
submit_result["pipelineRunId"]
if "pipelineRunId" in submit_result
else None
Expand Down Expand Up @@ -578,7 +578,7 @@ def wrapper(cls, *args, **kwargs):

if is_self_contained:
submit_result = pipeline_run.submit()
completion.pipeline_run_id = (
completion["pipelineRunId"] = (
submit_result["pipelineRunId"]
if "pipelineRunId" in submit_result
else None
Expand Down Expand Up @@ -630,7 +630,7 @@ async def wrapper(cls, *args, **kwargs):

if is_self_contained:
submit_result = pipeline_run.submit()
completion.pipeline_run_id = (
completion["pipelineRunId"] = (
submit_result["pipelineRunId"]
if "pipelineRunId" in submit_result
else None
Expand Down
Loading

0 comments on commit 8d449cf

Please sign in to comment.