Skip to content

Commit

Permalink
Merge pull request #919 from parea-ai/PAI-1201-cook-book-using-parea-…
Browse files Browse the repository at this point in the history
…on-class-and-test-using-decorator-on-fastapi-endpoint

organize cookbook into folders
  • Loading branch information
jalexanderII committed Jun 4, 2024
2 parents 0ef86f3 + 1f92a2e commit 5cfd670
Show file tree
Hide file tree
Showing 53 changed files with 104 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ You can define evaluation functions locally or use the ones you have deployed to
Parea's [Test Hub](https://app.parea.ai/test-hub).
If you choose the latter option, the evaluation happens asynchronously and non-blocking.

A fully locally working cookbook can be found [here](parea/cookbook/tracing_and_evaluating_openai_endpoint.py).
A fully locally working cookbook can be found [here](parea/cookbook/openai/tracing_and_evaluating_openai_endpoint.py).
Alternatively, you can add the following code to your codebase to get started:

```python
Expand Down
Empty file.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import asyncio
import os

import anthropic
from dotenv import load_dotenv

from parea import Parea
from parea.cookbook.data.anthropic_tool_use_examples import missing_information, multiple_tool_use, single_tool_use
from parea.cookbook.assets.data.anthropic_tool_use_examples import missing_information, multiple_tool_use, single_tool_use

load_dotenv()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
from typing import Optional

import base64
import json
import os

import requests
from anthropic import Anthropic
from dotenv import load_dotenv
from openai import OpenAI
Expand Down Expand Up @@ -28,13 +32,6 @@ def image_maker(query: str) -> str:
return image_url


from typing import Optional

import base64

import requests


@trace
def ask_vision(image_url: str) -> Optional[str]:
image_data = requests.get(image_url).content
Expand Down
Empty file.
File renamed without changes.
Empty file.
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
Empty file.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
54 changes: 54 additions & 0 deletions parea/cookbook/langchain/trace_class_call_method.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import os

from dotenv import load_dotenv
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI

from parea import Parea, trace
from parea.utils.trace_integrations.langchain import PareaAILangchainTracer

load_dotenv()

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


class LangChainModule:
handler = PareaAILangchainTracer()

def __init__(self):
self.llm = ChatOpenAI(openai_api_key=os.getenv("OPENAI_API_KEY"))

def get_chain(self):
prompt = ChatPromptTemplate.from_messages([("user", "{input}")])
chain = prompt | self.llm | StrOutputParser()
return chain

@trace(name="langchain_caller_call")
def __call__(self, query: str) -> str:
chain = self.get_chain()
return chain.invoke(
{"input": "Write a Hello World program in Python using FastAPI."},
config={"callbacks": [self.handler]},
)


class LLMCaller:
def __init__(self, query: str):
self.client = LangChainModule()
self.query = query

@trace(name="llm_caller_call")
def __call__(self) -> str:
return self.client(query=self.query)


@trace
def main(query: str) -> str:
caller = LLMCaller(query=query)
return caller()


if __name__ == "__main__":
result = main("Write a Hello World program in Python using FastAPI.")
print(result)
2 changes: 1 addition & 1 deletion parea/cookbook/langchain/trace_langchain_bedrock_rag.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@


def get_docs():
loader = TextLoader("../data/2022-letter.txt")
loader = TextLoader("../assets/data/2022-letter.txt")
letter = loader.load()
text_splitter = RecursiveCharacterTextSplitter(separators=["\n\n", "\n"], chunk_size=4000, chunk_overlap=100)
return text_splitter.split_documents(letter)
Expand Down
2 changes: 1 addition & 1 deletion parea/cookbook/langchain/trace_langchain_rag_agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

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

loader = TextLoader("../data/state_of_the_union.txt")
loader = TextLoader("../assets/data/state_of_the_union.txt")


documents = loader.load()
Expand Down
Empty file.
File renamed without changes.
Empty file.
39 changes: 39 additions & 0 deletions parea/cookbook/openai/trace_class_call_method.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from typing import List

import os

from dotenv import load_dotenv
from openai import OpenAI

from parea import Parea, trace

load_dotenv()

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


class LLMCaller:
def __init__(self, messages: List[dict[str, str]]):
self.messages = messages
self.client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
p.wrap_openai_client(self.client)

@trace
def __call__(self, model: str = "gpt-4o", temperature: float = 0.0) -> str:
return self.client.chat.completions.create(model=model, temperature=temperature, messages=self.messages).choices[0].message.content


@trace
def main(topic: str) -> str:
caller = LLMCaller(
messages=[
{"role": "system", "content": "You are a debater making an argument on a topic."},
{"role": "user", "content": f"The discussion topic is {topic}"},
]
)
return caller()


if __name__ == "__main__":
result = main("The impact of climate change on the economy")
print(result)
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from openai.lib.azure import AsyncAzureOpenAI, AzureOpenAI

from parea import Parea, trace
from parea.cookbook.data.openai_input_examples import functions_example, simple_example
from parea.cookbook.assets.data.openai_input_examples import functions_example, simple_example

load_dotenv()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from openai import AsyncOpenAI, OpenAI

from parea import Parea, trace
from parea.cookbook.data.openai_input_examples import functions_example, simple_example_json
from parea.cookbook.assets.data.openai_input_examples import functions_example, simple_example_json

load_dotenv()

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from dotenv import load_dotenv

from parea import Parea, aprocess_stream_and_yield, convert_openai_raw_to_log, process_stream_and_yield, trace
from parea.cookbook.data.openai_input_examples import functions_example, simple_example, tool_calling_example
from parea.cookbook.assets.data.openai_input_examples import functions_example, simple_example, tool_calling_example
from parea.wrapper import get_formatted_openai_response

load_dotenv()
Expand Down
Empty file.
Empty file.
File renamed without changes.
File renamed without changes.

0 comments on commit 5cfd670

Please sign in to comment.