-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #929 from parea-ai/new-cookbook
new cookbook, set openai.api_type
- Loading branch information
Showing
3 changed files
with
58 additions
and
1 deletion.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
parea/cookbook/evals_and_experiments/modify_dataset_before_experiment.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import os | ||
|
||
from dotenv import load_dotenv | ||
from openai import OpenAI | ||
|
||
from parea import Parea, trace | ||
from parea.evals.rag import context_query_relevancy_factory | ||
from parea.schemas import TestCase | ||
|
||
load_dotenv() | ||
|
||
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY")) | ||
p = Parea(api_key=os.getenv("PAREA_API_KEY")) | ||
p.wrap_openai_client(client) | ||
|
||
context_query_relevancy = context_query_relevancy_factory(model="gpt-4o", context_fields=["context"]) | ||
|
||
|
||
@trace(eval_funcs=[context_query_relevancy]) | ||
def run_experiment(question: str, context: str) -> str: | ||
return ( | ||
client.chat.completions.create( | ||
model="gpt-4o", | ||
temperature=0, | ||
messages=[{"role": "user", "content": f"Answer question using context. Context: {context}. Question: {question}"}], | ||
) | ||
.choices[0] | ||
.message.content | ||
) | ||
|
||
|
||
# You can fetch a dataset directly and then modify it to meet our needs before passing it to p.experiment. | ||
def rename_information_to_context(num_samples: int = 3): | ||
dataset = p.get_collection("Example_Dataset_Name") | ||
if dataset: | ||
testcases: list[TestCase] = list(dataset.test_cases.values()) | ||
# Assume dataset looks like this: | ||
# [ | ||
# inputs={"information": "Some long document", "question": "What is X?"}, target="X is Y" ... | ||
# ] | ||
return [{"context": case.inputs["information"], "question": case.inputs["question"], "target": case.target} for case in testcases[:num_samples]] | ||
return [] | ||
|
||
|
||
def main(): | ||
data = rename_information_to_context() | ||
experiment = p.experiment("My_Experiment_Name", func=run_experiment, data=data) | ||
experiment.run() | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ build-backend = "poetry.core.masonry.api" | |
[tool.poetry] | ||
name = "parea-ai" | ||
packages = [{ include = "parea" }] | ||
version = "0.2.171" | ||
version = "0.2.172" | ||
description = "Parea python sdk" | ||
readme = "README.md" | ||
authors = ["joel-parea-ai <[email protected]>"] | ||
|