Skip to content

Commit 28ca4ac

Browse files
committed
style: fix formating
1 parent c8d7730 commit 28ca4ac

File tree

3 files changed

+59
-25
lines changed

3 files changed

+59
-25
lines changed

src/examples/dspy_example/react.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,49 @@
33
import dspy
44

55
# Add the local src folder to the Python path
6-
sys.path.insert(0, os.path.abspath('/Users/karthikkalyanaraman/work/langtrace/langtrace-python-sdk/src'))
6+
sys.path.insert(
7+
0,
8+
os.path.abspath(
9+
"/Users/karthikkalyanaraman/work/langtrace/langtrace-python-sdk/src"
10+
),
11+
)
712

813
# flake8: noqa
914
from langtrace_python_sdk import langtrace, with_langtrace_root_span
15+
1016
langtrace.init()
1117

12-
turbo = dspy.OpenAI(model='gpt-3.5-turbo', max_tokens=250)
18+
turbo = dspy.OpenAI(model="gpt-3.5-turbo", max_tokens=250)
1319
dspy.settings.configure(lm=turbo)
1420

15-
colbertv2_wiki17_abstracts = dspy.ColBERTv2(url='http://20.102.90.50:2017/wiki17_abstracts')
21+
colbertv2_wiki17_abstracts = dspy.ColBERTv2(
22+
url="http://20.102.90.50:2017/wiki17_abstracts"
23+
)
1624
dspy.settings.configure(rm=colbertv2_wiki17_abstracts)
1725
retriever = dspy.Retrieve(k=3)
1826

27+
1928
# Define a simple signature for basic question answering
2029
class BasicQA(dspy.Signature):
2130
"""Answer questions with short factoid answers."""
31+
2232
question = dspy.InputField()
2333
answer = dspy.OutputField(desc="often between 1 and 5 words")
2434

35+
2536
@with_langtrace_root_span(name="react_example")
2637
def example():
2738

2839
# Pass signature to ReAct module
2940
react_module = dspy.ReAct(BasicQA)
3041

3142
# Call the ReAct module on a particular input
32-
question = 'Aside from the Apple Remote, what other devices can control the program Apple Remote was originally designed to interact with?'
43+
question = "Aside from the Apple Remote, what other devices can control the program Apple Remote was originally designed to interact with?"
3344
result = react_module(question=question)
3445

3546
print(f"Question: {question}")
3647
print(f"Final Predicted Answer (after ReAct process): {result.answer}")
3748

38-
if __name__ == '__main__':
49+
50+
if __name__ == "__main__":
3951
example()

src/langtrace_python_sdk/instrumentation/dspy/patch.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ def traced_method(wrapped, instance, args, kwargs):
3434
if args[0].prog:
3535
prog = {
3636
"name": args[0].prog.__class__.__name__,
37-
"signature": str(args[0].prog.signature) if args[0].prog.signature else None,
37+
"signature": (
38+
str(args[0].prog.signature) if args[0].prog.signature else None
39+
),
3840
}
3941
span_attributes["dspy.optimizer.module.prog"] = json.dumps(prog)
4042
if "metric" in instance and instance.metric:
@@ -60,9 +62,7 @@ def traced_method(wrapped, instance, args, kwargs):
6062
span_attributes["dspy.optimizer.config"] = json.dumps(config)
6163

6264
attributes = FrameworkSpanAttributes(**span_attributes)
63-
with tracer.start_as_current_span(
64-
operation_name, kind=SpanKind.CLIENT
65-
) as span:
65+
with tracer.start_as_current_span(operation_name, kind=SpanKind.CLIENT) as span:
6666
_set_input_attributes(span, kwargs, attributes)
6767

6868
try:
@@ -108,9 +108,7 @@ def traced_method(wrapped, instance, args, kwargs):
108108
span_attributes["dspy.signature.args"] = str(kwargs)
109109

110110
attributes = FrameworkSpanAttributes(**span_attributes)
111-
with tracer.start_as_current_span(
112-
operation_name, kind=SpanKind.CLIENT
113-
) as span:
111+
with tracer.start_as_current_span(operation_name, kind=SpanKind.CLIENT) as span:
114112
_set_input_attributes(span, kwargs, attributes)
115113

116114
try:
@@ -156,11 +154,15 @@ def traced_method(wrapped, instance, args, kwargs):
156154
if "num_threads" in instance and instance.num_threads is not None:
157155
span_attributes["dspy.evaluate.num_threads"] = str(instance.num_threads)
158156
if "return_outputs" in instance and instance.return_outputs is not None:
159-
span_attributes["dspy.evaluate.return_outputs"] = str(instance.return_outputs)
157+
span_attributes["dspy.evaluate.return_outputs"] = str(
158+
instance.return_outputs
159+
)
160160
if "display_table" in instance and instance.display_table is not None:
161161
span_attributes["dspy.evaluate.display_table"] = str(instance.display_table)
162162
if "display_progress" in instance and instance.display_progress is not None:
163-
span_attributes["dspy.evaluate.display_progress"] = str(instance.display_progress)
163+
span_attributes["dspy.evaluate.display_progress"] = str(
164+
instance.display_progress
165+
)
164166
if "metric" in instance and instance.metric is not None:
165167
span_attributes["dspy.evaluate.metric"] = instance.metric.__name__
166168
if "error_count" in instance and instance.error_count is not None:
@@ -173,9 +175,7 @@ def traced_method(wrapped, instance, args, kwargs):
173175
span_attributes["dspy.evaluate.args"] = str(args)
174176

175177
attributes = FrameworkSpanAttributes(**span_attributes)
176-
with tracer.start_as_current_span(
177-
operation_name, kind=SpanKind.CLIENT
178-
) as span:
178+
with tracer.start_as_current_span(operation_name, kind=SpanKind.CLIENT) as span:
179179
_set_input_attributes(span, kwargs, attributes)
180180

181181
try:

src/langtrace_python_sdk/instrumentation/openai/patch.py

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,10 @@ def traced_method(wrapped, instance, args, kwargs):
471471
attributes = LLMSpanAttributes(**span_attributes)
472472

473473
tools = []
474-
if kwargs.get("temperature") is not None and kwargs.get("temperature") != NOT_GIVEN:
474+
if (
475+
kwargs.get("temperature") is not None
476+
and kwargs.get("temperature") != NOT_GIVEN
477+
):
475478
attributes.llm_temperature = kwargs.get("temperature")
476479
if kwargs.get("top_p") is not None and kwargs.get("top_p") != NOT_GIVEN:
477480
attributes.llm_top_p = kwargs.get("top_p")
@@ -499,7 +502,11 @@ def traced_method(wrapped, instance, args, kwargs):
499502
try:
500503
# Attempt to call the original method
501504
result = wrapped(*args, **kwargs)
502-
if kwargs.get("stream") is False or kwargs.get("stream") is None or kwargs.get("stream") == NOT_GIVEN:
505+
if (
506+
kwargs.get("stream") is False
507+
or kwargs.get("stream") is None
508+
or kwargs.get("stream") == NOT_GIVEN
509+
):
503510
span.set_attribute("llm.model", result.model)
504511
if hasattr(result, "choices") and result.choices is not None:
505512
responses = [
@@ -528,7 +535,8 @@ def traced_method(wrapped, instance, args, kwargs):
528535
span.set_attribute("llm.responses", json.dumps(responses))
529536
if (
530537
hasattr(result, "system_fingerprint")
531-
and result.system_fingerprint is not None and result.system_fingerprint != NOT_GIVEN
538+
and result.system_fingerprint is not None
539+
and result.system_fingerprint != NOT_GIVEN
532540
):
533541
span.set_attribute(
534542
"llm.system.fingerprint", result.system_fingerprint
@@ -555,7 +563,10 @@ def traced_method(wrapped, instance, args, kwargs):
555563
)
556564

557565
# iterate over kwargs.get("functions") and calculate the prompt tokens
558-
if kwargs.get("functions") is not None and kwargs.get("functions") != NOT_GIVEN:
566+
if (
567+
kwargs.get("functions") is not None
568+
and kwargs.get("functions") != NOT_GIVEN
569+
):
559570
for function in kwargs.get("functions"):
560571
prompt_tokens += calculate_prompt_tokens(
561572
json.dumps(function), kwargs.get("model")
@@ -641,7 +652,10 @@ async def traced_method(wrapped, instance, args, kwargs):
641652
attributes = LLMSpanAttributes(**span_attributes)
642653

643654
tools = []
644-
if kwargs.get("temperature") is not None and kwargs.get("temperature") != NOT_GIVEN:
655+
if (
656+
kwargs.get("temperature") is not None
657+
and kwargs.get("temperature") != NOT_GIVEN
658+
):
645659
attributes.llm_temperature = kwargs.get("temperature")
646660
if kwargs.get("top_p") is not None and kwargs.get("top_p") != NOT_GIVEN:
647661
attributes.llm_top_p = kwargs.get("top_p")
@@ -667,7 +681,11 @@ async def traced_method(wrapped, instance, args, kwargs):
667681
try:
668682
# Attempt to call the original method
669683
result = await wrapped(*args, **kwargs)
670-
if kwargs.get("stream") is False or kwargs.get("stream") is None or kwargs.get("stream") == NOT_GIVEN:
684+
if (
685+
kwargs.get("stream") is False
686+
or kwargs.get("stream") is None
687+
or kwargs.get("stream") == NOT_GIVEN
688+
):
671689
span.set_attribute("llm.model", result.model)
672690
if hasattr(result, "choices") and result.choices is not None:
673691
responses = [
@@ -696,7 +714,8 @@ async def traced_method(wrapped, instance, args, kwargs):
696714
span.set_attribute("llm.responses", json.dumps(responses))
697715
if (
698716
hasattr(result, "system_fingerprint")
699-
and result.system_fingerprint is not None and result.system_fingerprint != NOT_GIVEN
717+
and result.system_fingerprint is not None
718+
and result.system_fingerprint != NOT_GIVEN
700719
):
701720
span.set_attribute(
702721
"llm.system.fingerprint", result.system_fingerprint
@@ -723,7 +742,10 @@ async def traced_method(wrapped, instance, args, kwargs):
723742
)
724743

725744
# iterate over kwargs.get("functions") and calculate the prompt tokens
726-
if kwargs.get("functions") is not None and kwargs.get("functions") != NOT_GIVEN:
745+
if (
746+
kwargs.get("functions") is not None
747+
and kwargs.get("functions") != NOT_GIVEN
748+
):
727749
for function in kwargs.get("functions"):
728750
prompt_tokens += calculate_prompt_tokens(
729751
json.dumps(function), kwargs.get("model")

0 commit comments

Comments
 (0)