Skip to content

Commit

Permalink
Merge pull request #86 from parea-ai/update-sdk
Browse files Browse the repository at this point in the history
update cookbook and examples, fix nits
  • Loading branch information
jalexanderII committed Sep 9, 2023
2 parents 7303d2c + b80148f commit e8a2a24
Show file tree
Hide file tree
Showing 11 changed files with 527 additions and 393 deletions.
46 changes: 8 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,61 +82,31 @@ async def main_async():

```python
import os
import time

import openai
from dotenv import load_dotenv

from parea import Parea
from parea.helpers import to_date_and_time_string, gen_trace_id
from parea.parea_logger import parea_logger
from parea.schemas.models import TraceLog, LLMInputs

load_dotenv()

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

p = Parea(api_key=os.getenv("PAREA_API_KEY"))

# define your OpenAI call as you would normally
x = "Golang"
y = "Fiber"
inputs = {"x": x, "y": y}
messages = [
{"role": "user", "content": f"Write a hello world program using {x} and the {y} framework."},
]
messages = [{
"role": "user",
"content": f"Write a hello world program using {x} and the {y} framework."
}]
model = "gpt-3.5-turbo"
model_params = {
"temperature": 0.7,
"top_p": 1.0,
}
model_config = {"model": model, "messages": messages, "model_params": model_params}
start_time = time.time()
completion = openai.ChatCompletion.create(model=model, messages=messages, **model_params)
output = completion.choices[0].message["content"]
end_time = time.time()

# the TraceLog schema
log_request = TraceLog(
trace_id=gen_trace_id(),
start_timestamp=to_date_and_time_string(start_time),
end_timestamp=to_date_and_time_string(end_time),
status="success",
trace_name="Test Log",
inputs=inputs,
configuration=LLMInputs(**model_config),
output=output,
input_tokens=completion.usage["prompt_tokens"],
output_tokens=completion.usage["completion_tokens"],
total_tokens=completion.usage["total_tokens"],
)
temperature = 0.0


# define your OpenAI call as you would normally and we'll automatically log the results
def main():
parea_logger.record_log(data=log_request)


async def main_async():
await parea_logger.arecord_log(data=log_request)
openai.ChatCompletion.create(model=model, temperature=temperature, messages=messages).choices[0].message["content"]
```

### Open source community features
Expand Down
6 changes: 3 additions & 3 deletions parea/client.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import asyncio
import time
from uuid import uuid4

from attrs import asdict, define, field

from parea.api_client import HTTPClient
from parea.helpers import gen_trace_id
from parea.parea_logger import parea_logger
from parea.schemas.models import Completion, CompletionResponse, FeedbackRequest, UseDeployedPrompt, UseDeployedPromptResponse
from parea.utils.trace_utils import default_logger, get_current_trace_id, trace_data
Expand All @@ -24,7 +24,7 @@ def __attrs_post_init__(self):
parea_logger.set_client(self._client)

def completion(self, data: Completion) -> CompletionResponse:
inference_id = str(uuid4())
inference_id = gen_trace_id()
data.inference_id = inference_id
r = self._client.request(
"POST",
Expand All @@ -37,7 +37,7 @@ def completion(self, data: Completion) -> CompletionResponse:
return CompletionResponse(**r.json())

async def acompletion(self, data: Completion) -> CompletionResponse:
inference_id = str(uuid4())
inference_id = gen_trace_id()
data.inference_id = inference_id
r = await self._client.request_async(
"POST",
Expand Down
38 changes: 20 additions & 18 deletions parea/cookbook/tracing_with_Parea_sdk.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@
"def argument_generator(query: str, additional_description: str = \"\") -> str:\n",
" return p.completion(\n",
" Completion(\n",
" deployment_id=\"p-RG8d9rfJc_0cctwfpb_n6\",\n",
" deployment_id=\"p-XOh3kp8B0nIE82WgioPnr\",\n",
" llm_inputs={\n",
" \"additional_description\": additional_description,\n",
" \"date\": f\"{datetime.now()}\",\n",
Expand All @@ -127,21 +127,21 @@
"def critic(argument: str) -> str:\n",
" return p.completion(\n",
" Completion(\n",
" deployment_id=\"p-fXgZytT3dJjXD_71TDR4s\",\n",
" deployment_id=\"p-PSOwRyIPaQRq4xQW3MbpV\",\n",
" llm_inputs={\"argument\": argument},\n",
" )\n",
" ).content\n",
"\n",
"\n",
"def refiner(query: str, additional_description: str, current_arg: str, criticism: str) -> str:\n",
"def refiner(query: str, additional_description: str, argument: str, criticism: str) -> str:\n",
" return p.completion(\n",
" Completion(\n",
" deployment_id=\"p--G2s9okMTvBEh3d8YqLY2\",\n",
" deployment_id=\"p-bJ3-UKh9-ixapZafaRBsj\",\n",
" llm_inputs={\n",
" \"additional_description\": additional_description,\n",
" \"date\": f\"{datetime.now()}\",\n",
" \"query\": query,\n",
" \"current_arg\": current_arg,\n",
" \"argument\": argument,\n",
" \"criticism\": criticism,\n",
" },\n",
" )\n",
Expand Down Expand Up @@ -285,15 +285,15 @@
"\n",
"\n",
"# let's return the full CompletionResponse to see what other information is returned\n",
"def refiner2(query: str, additional_description: str, current_arg: str, criticism: str) -> CompletionResponse:\n",
"def refiner2(query: str, additional_description: str, argument: str, criticism: str) -> CompletionResponse:\n",
" return p.completion(\n",
" Completion(\n",
" deployment_id=\"p-8Er1Xo0GDGF2xtpmMOpbn\",\n",
" deployment_id=\"p-bJ3-UKh9-ixapZafaRBsj\",\n",
" llm_inputs={\n",
" \"additional_description\": additional_description,\n",
" \"date\": f\"{datetime.now()}\",\n",
" \"query\": query,\n",
" \"current_arg\": current_arg,\n",
" \"argument\": argument,\n",
" \"criticism\": criticism,\n",
" },\n",
" )\n",
Expand Down Expand Up @@ -337,35 +337,37 @@
},
"outputs": [],
"source": [
"from typing import Tuple\n",
"\n",
"\n",
"# you can also add metadata and tags via the decorator\n",
"@trace(\n",
" tags=[\"cookbook-example-deployed\", \"feedback_tracked-deployed\"],\n",
" metadata={\"source\": \"python-sdk\", \"deployed\": True},\n",
" metadata={\"source\": \"python-sdk\", \"deployed\": \"True\"},\n",
")\n",
"def argument_chain3(query: str, additional_description: str = \"\") -> CompletionResponse:\n",
"def argument_chain_tags_metadata(query: str, additional_description: str = \"\") -> Tuple[CompletionResponse, str]:\n",
" trace_id = get_current_trace_id() # get parent's trace_id\n",
" argument = argument_generator(query, additional_description)\n",
" criticism = critic(argument)\n",
" return refiner2(query, additional_description, argument, criticism)"
" return refiner2(query, additional_description, argument, criticism), trace_id"
]
},
{
"cell_type": "code",
"source": [
"import json, attrs\n",
"\n",
"result = argument_chain3(\n",
"result2, trace_id = argument_chain_tags_metadata(\n",
" \"Whether coffee is good for you.\",\n",
" additional_description=\"Provide a concise, few sentence argument on why coffee is good for you.\",\n",
")\n",
"\n",
"print(json.dumps(attrs.asdict(result), indent=4))\n",
"print(json.dumps(attrs.asdict(result2), indent=2))\n",
"\n",
"p.record_feedback(\n",
" FeedbackRequest(\n",
" # we can access the inference_id (same as trace_id) from the CompletionResponse object directly\n",
" trace_id=result.inference_id,\n",
" score=0.5,\n",
" target=\"Coffee is nice. Full stop.\",\n",
" trace_id=trace_id,\n",
" score=0.7, # 0.0 (bad) to 1.0 (good)\n",
" target=\"Coffee is wonderful. End of story.\",\n",
" )\n",
")"
],
Expand Down
6 changes: 4 additions & 2 deletions parea/cookbook/tracing_with_agent.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import os
import random
import time

from dotenv import load_dotenv

from parea import Parea
from parea.helpers import to_date_and_time_string
from parea.schemas.models import Completion, CompletionResponse, FeedbackRequest, LLMInputs, Message, ModelParams, Role
from parea.utils.trace_utils import get_current_trace_id, trace

load_dotenv()

p = Parea(api_key=os.getenv("PAREA_API_KEY"))
p = Parea(api_key=os.getenv("DEV_API_KEY"))

# Parea SDK makes it easy to use different LLMs with the same apis structure and standardized request/response schemas.
LLM_OPTIONS = [("gpt-3.5-turbo", "openai"), ("gpt-4", "openai"), ("claude-instant-1", "anthropic"), ("claude-2", "anthropic")]
Expand Down Expand Up @@ -84,7 +86,7 @@ def generate_tasks(main_objective: str, expounded_initial_task: list[dict[str, s
return new_tasks_list


@trace
@trace(name=f"run_agent-{to_date_and_time_string(time.time())}") # You can provide a custom name other than the function name
def run_agent(main_objective: str, initial_task: str = "") -> tuple[list[dict[str, str]], str]:
trace_id = get_current_trace_id()
generated_tasks = []
Expand Down
50 changes: 16 additions & 34 deletions parea/cookbook/tracing_with_deployed_prompt.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from typing import Tuple

import json
import os
from datetime import datetime

from attrs import asdict
from dotenv import load_dotenv

from parea import Parea
Expand All @@ -11,13 +13,13 @@

load_dotenv()

p = Parea(api_key=os.getenv("PAREA_API_KEY"))
p = Parea(api_key=os.getenv("DEV_API_KEY"))


def deployed_argument_generator(query: str, additional_description: str = "") -> str:
return p.completion(
Completion(
deployment_id="p-RG8d9rfJc_0cctwfpb_n6",
deployment_id="p-XOh3kp8B0nIE82WgioPnr",
llm_inputs={
"additional_description": additional_description,
"date": f"{datetime.now()}",
Expand All @@ -30,7 +32,7 @@ def deployed_argument_generator(query: str, additional_description: str = "") ->
def deployed_critic(argument: str) -> str:
return p.completion(
Completion(
deployment_id="p-fXgZytT3dJjXD_71TDR4s",
deployment_id="p-PSOwRyIPaQRq4xQW3MbpV",
llm_inputs={"argument": argument},
)
).content
Expand All @@ -39,12 +41,12 @@ def deployed_critic(argument: str) -> str:
def deployed_refiner(query: str, additional_description: str, current_arg: str, criticism: str) -> str:
return p.completion(
Completion(
deployment_id="p--G2s9okMTvBEh3d8YqLY2",
deployment_id="p-bJ3-UKh9-ixapZafaRBsj",
llm_inputs={
"additional_description": additional_description,
"date": f"{datetime.now()}",
"query": query,
"current_arg": current_arg,
"argument": current_arg,
"criticism": criticism,
},
)
Expand All @@ -54,12 +56,12 @@ def deployed_refiner(query: str, additional_description: str, current_arg: str,
def deployed_refiner2(query: str, additional_description: str, current_arg: str, criticism: str) -> CompletionResponse:
return p.completion(
Completion(
deployment_id="p--G2s9okMTvBEh3d8YqLY2",
deployment_id="p-bJ3-UKh9-ixapZafaRBsj",
llm_inputs={
"additional_description": additional_description,
"date": f"{datetime.now()}",
"query": query,
"current_arg": current_arg,
"argument": current_arg,
"criticism": criticism,
},
)
Expand All @@ -73,22 +75,15 @@ def deployed_argument_chain(query: str, additional_description: str = "") -> str
return deployed_refiner(query, additional_description, argument, criticism)


@trace
def deployed_argument_chain2(query: str, additional_description: str = "") -> Tuple[str, str]:
trace_id = get_current_trace_id()
argument = deployed_argument_generator(query, additional_description)
criticism = deployed_critic(argument)
return deployed_refiner(query, additional_description, argument, criticism), trace_id


@trace(
tags=["cookbook-example-deployed", "feedback_tracked-deployed"],
metadata={"source": "python-sdk", "deployed": True},
metadata={"source": "python-sdk", "deployed": "True"},
)
def deployed_argument_chain_tags_metadata(query: str, additional_description: str = "") -> CompletionResponse:
def deployed_argument_chain_tags_metadata(query: str, additional_description: str = "") -> tuple[CompletionResponse, str]:
trace_id = get_current_trace_id() # get parent's trace_id
argument = deployed_argument_generator(query, additional_description)
criticism = deployed_critic(argument)
return deployed_refiner2(query, additional_description, argument, criticism)
return deployed_refiner2(query, additional_description, argument, criticism), trace_id


if __name__ == "__main__":
Expand All @@ -98,27 +93,14 @@ def deployed_argument_chain_tags_metadata(query: str, additional_description: st
)
print(result1)

result2, trace_id2 = deployed_argument_chain2(
"Whether wine is good for you.",
additional_description="Provide a concise, few sentence argument on why wine is good for you.",
)
print(result2)
p.record_feedback(
FeedbackRequest(
trace_id=trace_id2,
score=0.0, # 0.0 (bad) to 1.0 (good)
target="Moonshine is wonderful.",
)
)

result3 = deployed_argument_chain_tags_metadata(
result2, trace_id = deployed_argument_chain_tags_metadata(
"Whether coffee is good for you.",
additional_description="Provide a concise, few sentence argument on why coffee is good for you.",
)
print(result3.content)
print(json.dumps(asdict(result2), indent=2))
p.record_feedback(
FeedbackRequest(
trace_id=result3.inference_id,
trace_id=trace_id,
score=0.7, # 0.0 (bad) to 1.0 (good)
target="Coffee is wonderful. End of story.",
)
Expand Down
Loading

0 comments on commit e8a2a24

Please sign in to comment.