Skip to content

Commit a44edd0

Browse files
committed
feat: attach to parea client and create cookbook
1 parent 8b80208 commit a44edd0

File tree

2 files changed

+157
-1
lines changed

2 files changed

+157
-1
lines changed

parea/client.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def wrap_anthropic_client(self, client: "Anthropic", integration: Optional[str]
9595
if integration:
9696
self._client.add_integration(integration)
9797

98-
def auto_trace_openai_clients(self) -> None:
98+
def auto_trace_openai_clients(self, integration: Optional[str]) -> None:
9999
import openai
100100

101101
openai._ModuleClient = patch_openai_client_classes(openai._ModuleClient, self)
@@ -104,6 +104,24 @@ def auto_trace_openai_clients(self) -> None:
104104
openai.AzureOpenAI = patch_openai_client_classes(openai.AzureOpenAI, self)
105105
openai.AsyncAzureOpenAI = patch_openai_client_classes(openai.AsyncAzureOpenAI, self)
106106

107+
if integration:
108+
self._client.add_integration(integration)
109+
110+
def trace_dspy(self):
111+
from parea.utils.trace_integrations.dspy import DSPyInstrumentor
112+
113+
try:
114+
import openai
115+
116+
if openai.version.__version__.startswith('0.'):
117+
self.wrap_openai_client(openai, "dspy")
118+
else:
119+
self.auto_trace_openai_clients("dspy")
120+
except ImportError:
121+
pass
122+
123+
DSPyInstrumentor().instrument()
124+
107125
def integrate_with_sglang(self):
108126
self.auto_trace_openai_clients()
109127
self._client.add_integration("sglang")
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
import os
2+
3+
import dspy
4+
from dotenv import load_dotenv
5+
6+
from parea import Parea
7+
8+
load_dotenv()
9+
10+
p = Parea(api_key=os.getenv("PAREA_API_KEY"), project_name="testing")
11+
p.trace_dspy()
12+
13+
gpt3_turbo = dspy.OpenAI(model="gpt-3.5-turbo-1106", max_tokens=300)
14+
dspy.configure(lm=gpt3_turbo)
15+
# print(gpt3_turbo("hello! this is a raw prompt to GPT-3.5."))
16+
17+
# Define a retrieval model server to send retrieval requests to
18+
colbertv2_wiki17_abstracts = dspy.ColBERTv2(url="http://20.102.90.50:2017/wiki17_abstracts")
19+
20+
# Configure retrieval server internally
21+
dspy.settings.configure(rm=colbertv2_wiki17_abstracts)
22+
23+
24+
#Define a simple signature for basic question answering
25+
class BasicQA(dspy.Signature):
26+
"""Answer questions with short factoid answers."""
27+
question = dspy.InputField()
28+
answer = dspy.OutputField(desc="often between 1 and 5 words")
29+
30+
# Pass signature to Predict module
31+
generate_answer = dspy.Predict(BasicQA)
32+
33+
# Call the predictor on a particular input.
34+
question='What is the color of the sky?'
35+
pred = generate_answer(question=question)
36+
37+
print(f"Question: {question}")
38+
print(f"Predicted Answer: {pred.answer}")
39+
question = "What's something great about the ColBERT retrieval model ?!?abc"
40+
41+
# 1) Declare with a signature, and pass some config.
42+
classify = dspy.ChainOfThought('question -> answer', n=1)
43+
44+
# 2) Call with input argument.
45+
response = classify(question=question)
46+
47+
# 3) Access the outputs.
48+
print(response.completions.answer)
49+
50+
51+
# Define a simple signature for basic question answering
52+
class BasicQA(dspy.Signature):
53+
"""Answer questions with short factoid answers."""
54+
question = dspy.InputField()
55+
answer = dspy.OutputField(desc="often between 1 and 5 words")
56+
57+
#Pass signature to ChainOfThought module
58+
generate_answer = dspy.ChainOfThought(BasicQA)
59+
60+
# Call the predictor on a particular input.
61+
question='What is the color of the sky?12'
62+
pred = generate_answer(question=question)
63+
64+
print(f"Question: {question}")
65+
print(f"Predicted Answer: {pred.answer}")
66+
67+
68+
class BasicQA(dspy.Signature):
69+
"""Answer questions with short factoid answers."""
70+
question = dspy.InputField()
71+
answer = dspy.OutputField(desc="often between 1 and 5 words")
72+
73+
# Example completions generated by a model for reference
74+
completions = [
75+
dspy.Prediction(rationale=" I recall that during clear days, the sky often appears this colo12r", answer="blue"),
76+
dspy.Prediction(rationale=" Based on common knowledge, I believe the sky is typically seen 12as this color", answer="green"),
77+
dspy.Prediction(rationale=" From images and depictions in media, the sky is frequently42 represented with this hue", answer="blue"),
78+
]
79+
80+
# Pass signature to MultiChainComparison module
81+
compare_answers = dspy.MultiChainComparison(BasicQA)
82+
83+
# Call the MultiChainComparison on the completions
84+
question = ' What is the color of th e sky14?'
85+
final_pred = compare_answers(completions, question=question)
86+
87+
print(f"Question: {question}")
88+
print(f"Final Predicted Answer (after comparison): {final_pred.answer}")
89+
print(f"Final Rationale: {final_pred.rationale}")
90+
91+
92+
#Define a simple signature for basic question answering
93+
class GenerateAnswer(dspy.Signature):
94+
"""Answer questions with short factoid answers."""
95+
question = dspy.InputField()
96+
answer = dspy.OutputField(desc="often between 1 and 5 words")
97+
98+
# Pass signature to ProgramOfThought Module
99+
pot = dspy.ProgramOfThought(GenerateAnswer)
100+
101+
#Call the ProgramOfThought module on a particular input
102+
question = 'Sarah has 5 applez. She buys 123 more apples from the store. How many apples does Sarah have now?'
103+
result = pot(question=question)
104+
105+
print(f"Question: {question}")
106+
print(f"Final Predicted Answer (after ProgramOfThought process): {result.answer}")
107+
108+
109+
# Define a simple signature for basic question answering
110+
class BasicQA(dspy.Signature):
111+
"""Answer questions with short factoid answers."""
112+
113+
question = dspy.InputField()
114+
answer = dspy.OutputField(desc="often between 1 and 5 words")
115+
116+
117+
# Pass signature to ReAct module
118+
react_module = dspy.ReAct(BasicQA, tools=[])
119+
120+
# Call the ReAct module on a particular input
121+
question = "What is the color of the 2 skies?"
122+
result = react_module(question=question)
123+
124+
print(f"Question: {question}")
125+
print(f"Final Predicted Answer (after ReAct process): {result.answer}")
126+
127+
128+
query='Where was the first FIFA World Cup held?12'
129+
130+
131+
# Call the retriever on a particular query.
132+
retrieve = dspy.Retrieve(k=3)
133+
topK_passages = retrieve(query).passages
134+
135+
print(f"Top {retrieve.k} passages for question: {query} \n", '-' * 30, '\n')
136+
137+
for idx, passage in enumerate(topK_passages):
138+
print(f'{idx+1}]', passage, '\n')

0 commit comments

Comments
 (0)