langtrace-python-sdk 1.2.25__py3-none-any.whl → 1.3.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- examples/cohere_example/__init__.py +0 -0
- examples/cohere_example/chat.py +26 -0
- examples/cohere_example/chat_stream.py +23 -0
- examples/cohere_example/embed_create.py +21 -0
- examples/fastapi_example/basic_route.py +38 -0
- langtrace_python_sdk/constants/instrumentation/cohere.py +17 -0
- langtrace_python_sdk/constants/instrumentation/common.py +1 -0
- langtrace_python_sdk/instrumentation/cohere/__init__.py +0 -0
- langtrace_python_sdk/instrumentation/cohere/instrumentation.py +53 -0
- langtrace_python_sdk/instrumentation/cohere/patch.py +370 -0
- langtrace_python_sdk/instrumentation/openai/patch.py +4 -6
- langtrace_python_sdk/langtrace.py +5 -0
- langtrace_python_sdk/version.py +1 -1
- {langtrace_python_sdk-1.2.25.dist-info → langtrace_python_sdk-1.3.1.dist-info}/METADATA +2 -1
- {langtrace_python_sdk-1.2.25.dist-info → langtrace_python_sdk-1.3.1.dist-info}/RECORD +17 -8
- {langtrace_python_sdk-1.2.25.dist-info → langtrace_python_sdk-1.3.1.dist-info}/WHEEL +0 -0
- {langtrace_python_sdk-1.2.25.dist-info → langtrace_python_sdk-1.3.1.dist-info}/licenses/LICENSE +0 -0
|
File without changes
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
from dotenv import find_dotenv, load_dotenv
|
|
2
|
+
import cohere
|
|
3
|
+
|
|
4
|
+
from langtrace_python_sdk import langtrace
|
|
5
|
+
# from langtrace_python_sdk.utils.with_root_span import with_langtrace_root_span
|
|
6
|
+
|
|
7
|
+
_ = load_dotenv(find_dotenv())
|
|
8
|
+
|
|
9
|
+
langtrace.init(batch=False, debug_log_to_console=True, write_to_langtrace_cloud=False)
|
|
10
|
+
|
|
11
|
+
co = cohere.Client()
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
# @with_langtrace_root_span("chat_create")
|
|
15
|
+
def chat_comp():
|
|
16
|
+
response = co.chat(
|
|
17
|
+
chat_history=[
|
|
18
|
+
{"role": "USER", "message": "Who discovered gravity?"},
|
|
19
|
+
{"role": "CHATBOT", "message": "The man who is widely credited with discovering gravity is Sir Isaac Newton"}
|
|
20
|
+
],
|
|
21
|
+
message="What is today's news?",
|
|
22
|
+
# preamble="answer like yoda",
|
|
23
|
+
# perform web search before answering the question. You can also use your own custom connector.
|
|
24
|
+
# connectors=[{"id": "web-search"}]
|
|
25
|
+
)
|
|
26
|
+
print(response)
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
from dotenv import find_dotenv, load_dotenv
|
|
2
|
+
import cohere
|
|
3
|
+
|
|
4
|
+
from langtrace_python_sdk import langtrace
|
|
5
|
+
# from langtrace_python_sdk.utils.with_root_span import with_langtrace_root_span
|
|
6
|
+
|
|
7
|
+
_ = load_dotenv(find_dotenv())
|
|
8
|
+
|
|
9
|
+
langtrace.init(batch=False, debug_log_to_console=True, write_to_langtrace_cloud=False)
|
|
10
|
+
|
|
11
|
+
co = cohere.Client()
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
# @with_langtrace_root_span("chat_stream")
|
|
15
|
+
def chat_stream():
|
|
16
|
+
result = []
|
|
17
|
+
for event in co.chat_stream(message="Tell me a short story in 2 lines"):
|
|
18
|
+
if event.event_type == "text-generation":
|
|
19
|
+
result.append(event.text)
|
|
20
|
+
elif event.event_type == "stream-end":
|
|
21
|
+
break
|
|
22
|
+
print("".join(result))
|
|
23
|
+
return result
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
from dotenv import find_dotenv, load_dotenv
|
|
2
|
+
import cohere
|
|
3
|
+
|
|
4
|
+
from langtrace_python_sdk import langtrace
|
|
5
|
+
# from langtrace_python_sdk.utils.with_root_span import with_langtrace_root_span
|
|
6
|
+
|
|
7
|
+
_ = load_dotenv(find_dotenv())
|
|
8
|
+
|
|
9
|
+
langtrace.init(batch=False, debug_log_to_console=True, write_to_langtrace_cloud=False)
|
|
10
|
+
|
|
11
|
+
co = cohere.Client()
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
# @with_langtrace_root_span("embed_create")
|
|
15
|
+
def embed_create():
|
|
16
|
+
response = co.embed(
|
|
17
|
+
texts=['hello', 'goodbye'],
|
|
18
|
+
model='embed-english-v3.0',
|
|
19
|
+
input_type='classification'
|
|
20
|
+
)
|
|
21
|
+
# print(response)
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
from fastapi import FastAPI
|
|
2
|
+
from langchain_community.vectorstores.faiss import FAISS
|
|
3
|
+
from langchain_core.output_parsers import StrOutputParser
|
|
4
|
+
from langchain_core.prompts.chat import ChatPromptTemplate
|
|
5
|
+
from langchain_core.runnables import RunnablePassthrough
|
|
6
|
+
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
|
|
7
|
+
from openai import OpenAI
|
|
8
|
+
|
|
9
|
+
from langtrace_python_sdk import langtrace
|
|
10
|
+
|
|
11
|
+
langtrace.init(write_to_langtrace_cloud=False, debug_log_to_console=True)
|
|
12
|
+
app = FastAPI()
|
|
13
|
+
client = OpenAI()
|
|
14
|
+
|
|
15
|
+
@app.get("/")
|
|
16
|
+
def root():
|
|
17
|
+
vectorstore = FAISS.from_texts(
|
|
18
|
+
["Langtrace helps you ship high quality AI Apps to production."], embedding=OpenAIEmbeddings()
|
|
19
|
+
)
|
|
20
|
+
retriever = vectorstore.as_retriever()
|
|
21
|
+
|
|
22
|
+
template = """Answer the question based only on the following context:{context}
|
|
23
|
+
|
|
24
|
+
Question: {question}
|
|
25
|
+
"""
|
|
26
|
+
prompt = ChatPromptTemplate.from_template(template)
|
|
27
|
+
|
|
28
|
+
model = ChatOpenAI()
|
|
29
|
+
|
|
30
|
+
chain = (
|
|
31
|
+
{"context": retriever, "question": RunnablePassthrough()}
|
|
32
|
+
| prompt
|
|
33
|
+
| model
|
|
34
|
+
| StrOutputParser()
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
res = chain.invoke("How is Langtrace useful?")
|
|
38
|
+
return {"response": res}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
APIS = {
|
|
2
|
+
"CHAT_CREATE": {
|
|
3
|
+
"URL": "https://api.cohere.ai",
|
|
4
|
+
"METHOD": "cohere.client.chat",
|
|
5
|
+
"ENDPOINT": "/v1/chat",
|
|
6
|
+
},
|
|
7
|
+
"EMBED_CREATE": {
|
|
8
|
+
"URL": "https://api.cohere.ai",
|
|
9
|
+
"METHOD": "cohere.client.embed",
|
|
10
|
+
"ENDPOINT": "/v1/embed",
|
|
11
|
+
},
|
|
12
|
+
"CHAT_STREAM": {
|
|
13
|
+
"URL": "https://api.cohere.ai",
|
|
14
|
+
"METHOD": "cohere.client.chat_stream",
|
|
15
|
+
"ENDPOINT": "/v1/messages",
|
|
16
|
+
},
|
|
17
|
+
}
|
|
File without changes
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Instrumentation for Cohere
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
import importlib.metadata
|
|
6
|
+
from typing import Collection
|
|
7
|
+
|
|
8
|
+
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
|
|
9
|
+
from opentelemetry.trace import get_tracer
|
|
10
|
+
from wrapt import wrap_function_wrapper
|
|
11
|
+
|
|
12
|
+
from langtrace_python_sdk.instrumentation.cohere.patch import (
|
|
13
|
+
chat_create,
|
|
14
|
+
chat_stream,
|
|
15
|
+
embed_create
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
class CohereInstrumentation(BaseInstrumentor):
|
|
19
|
+
"""
|
|
20
|
+
The CohereInstrumentation class represents the Anthropic instrumentation
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
def instrumentation_dependencies(self) -> Collection[str]:
|
|
24
|
+
return ["cohere >= 5.0.0"]
|
|
25
|
+
|
|
26
|
+
def _instrument(self, **kwargs):
|
|
27
|
+
tracer_provider = kwargs.get("tracer_provider")
|
|
28
|
+
tracer = get_tracer(__name__, "", tracer_provider)
|
|
29
|
+
version = importlib.metadata.version("cohere")
|
|
30
|
+
|
|
31
|
+
wrap_function_wrapper(
|
|
32
|
+
"cohere.client",
|
|
33
|
+
"Client.chat",
|
|
34
|
+
chat_create("cohere.client.chat", version, tracer),
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
wrap_function_wrapper(
|
|
38
|
+
"cohere.client",
|
|
39
|
+
"Client.chat_stream",
|
|
40
|
+
chat_stream("cohere.client.chat_stream", version, tracer),
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
wrap_function_wrapper(
|
|
44
|
+
"cohere.client",
|
|
45
|
+
"Client.embed",
|
|
46
|
+
embed_create("cohere.client.embed", version, tracer),
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
def _instrument_module(self, module_name):
|
|
50
|
+
pass
|
|
51
|
+
|
|
52
|
+
def _uninstrument(self, **kwargs):
|
|
53
|
+
pass
|
|
@@ -0,0 +1,370 @@
|
|
|
1
|
+
"""
|
|
2
|
+
This module contains the patching logic for the Anthropic library."""
|
|
3
|
+
|
|
4
|
+
import json
|
|
5
|
+
|
|
6
|
+
from langtrace.trace_attributes import Event, LLMSpanAttributes
|
|
7
|
+
from opentelemetry import baggage
|
|
8
|
+
from opentelemetry.trace import SpanKind
|
|
9
|
+
from opentelemetry.trace.status import Status, StatusCode
|
|
10
|
+
|
|
11
|
+
from langtrace_python_sdk.constants.instrumentation.cohere import APIS
|
|
12
|
+
from langtrace_python_sdk.constants.instrumentation.common import (LANGTRACE_ADDITIONAL_SPAN_ATTRIBUTES_KEY, SERVICE_PROVIDERS)
|
|
13
|
+
from langtrace_python_sdk.utils.llm import estimate_tokens
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def embed_create(original_method, version, tracer):
|
|
17
|
+
"""Wrap the `embed_create` method."""
|
|
18
|
+
|
|
19
|
+
def traced_method(wrapped, instance, args, kwargs):
|
|
20
|
+
service_provider = SERVICE_PROVIDERS["COHERE"]
|
|
21
|
+
extra_attributes = baggage.get_baggage(LANGTRACE_ADDITIONAL_SPAN_ATTRIBUTES_KEY)
|
|
22
|
+
|
|
23
|
+
span_attributes = {
|
|
24
|
+
"langtrace.sdk.name": "langtrace-python-sdk",
|
|
25
|
+
"langtrace.service.name": service_provider,
|
|
26
|
+
"langtrace.service.type": "llm",
|
|
27
|
+
"langtrace.service.version": version,
|
|
28
|
+
"langtrace.version": "1.0.0",
|
|
29
|
+
"url.full": APIS["EMBED_CREATE"]["URL"],
|
|
30
|
+
"llm.api": APIS["EMBED_CREATE"]["ENDPOINT"],
|
|
31
|
+
"llm.model": kwargs.get("model"),
|
|
32
|
+
"llm.prompts": "",
|
|
33
|
+
"llm.embedding_dataset_id": kwargs.get("dataset_id"),
|
|
34
|
+
"llm.embedding_input_type": kwargs.get("input_type"),
|
|
35
|
+
"llm.embedding_job_name": kwargs.get("name"),
|
|
36
|
+
**(extra_attributes if extra_attributes is not None else {})
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
attributes = LLMSpanAttributes(**span_attributes)
|
|
40
|
+
|
|
41
|
+
if kwargs.get("user") is not None:
|
|
42
|
+
attributes.llm_user = kwargs.get("user")
|
|
43
|
+
|
|
44
|
+
span = tracer.start_span(
|
|
45
|
+
APIS["EMBED_CREATE"]["METHOD"], kind=SpanKind.CLIENT
|
|
46
|
+
)
|
|
47
|
+
for field, value in attributes.model_dump(by_alias=True).items():
|
|
48
|
+
if value is not None:
|
|
49
|
+
span.set_attribute(field, value)
|
|
50
|
+
try:
|
|
51
|
+
# Attempt to call the original method
|
|
52
|
+
result = wrapped(*args, **kwargs)
|
|
53
|
+
span.set_status(StatusCode.OK)
|
|
54
|
+
span.end()
|
|
55
|
+
return result
|
|
56
|
+
|
|
57
|
+
except Exception as error:
|
|
58
|
+
span.record_exception(error)
|
|
59
|
+
span.set_status(Status(StatusCode.ERROR, str(error)))
|
|
60
|
+
span.end()
|
|
61
|
+
raise
|
|
62
|
+
return traced_method
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def chat_create(original_method, version, tracer):
|
|
66
|
+
"""Wrap the `chat_create` method."""
|
|
67
|
+
|
|
68
|
+
def traced_method(wrapped, instance, args, kwargs):
|
|
69
|
+
service_provider = SERVICE_PROVIDERS["COHERE"]
|
|
70
|
+
|
|
71
|
+
message = kwargs.get("message", "")
|
|
72
|
+
prompts = json.dumps([
|
|
73
|
+
{
|
|
74
|
+
"role": "USER",
|
|
75
|
+
"content": message
|
|
76
|
+
}
|
|
77
|
+
])
|
|
78
|
+
preamble = kwargs.get("preamble")
|
|
79
|
+
if preamble:
|
|
80
|
+
prompts = json.dumps(
|
|
81
|
+
[{"role": "system", "content": preamble}] + [{"role": "USER", "content": message}]
|
|
82
|
+
)
|
|
83
|
+
|
|
84
|
+
chat_history = kwargs.get("chat_history")
|
|
85
|
+
if chat_history:
|
|
86
|
+
history = [
|
|
87
|
+
{
|
|
88
|
+
"message": {
|
|
89
|
+
"role": (
|
|
90
|
+
item.get("role") if item.get("role") is not None else "USER"
|
|
91
|
+
),
|
|
92
|
+
"content": (
|
|
93
|
+
item.get("message") if item.get("message") is not None else ""
|
|
94
|
+
)
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
for item in chat_history
|
|
98
|
+
]
|
|
99
|
+
prompts = prompts + json.dumps(history)
|
|
100
|
+
|
|
101
|
+
extra_attributes = baggage.get_baggage(LANGTRACE_ADDITIONAL_SPAN_ATTRIBUTES_KEY)
|
|
102
|
+
|
|
103
|
+
span_attributes = {
|
|
104
|
+
"langtrace.sdk.name": "langtrace-python-sdk",
|
|
105
|
+
"langtrace.service.name": service_provider,
|
|
106
|
+
"langtrace.service.type": "llm",
|
|
107
|
+
"langtrace.service.version": version,
|
|
108
|
+
"langtrace.version": "1.0.0",
|
|
109
|
+
"url.full": APIS["CHAT_CREATE"]["URL"],
|
|
110
|
+
"llm.api": APIS["CHAT_CREATE"]["ENDPOINT"],
|
|
111
|
+
"llm.model": kwargs.get("model") if kwargs.get("model") is not None else "command-r",
|
|
112
|
+
"llm.stream": False,
|
|
113
|
+
"llm.prompts": prompts,
|
|
114
|
+
**(extra_attributes if extra_attributes is not None else {})
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
attributes = LLMSpanAttributes(**span_attributes)
|
|
118
|
+
|
|
119
|
+
if kwargs.get("temperature") is not None:
|
|
120
|
+
attributes.llm_temperature = kwargs.get("temperature")
|
|
121
|
+
if kwargs.get("max_tokens") is not None:
|
|
122
|
+
attributes.max_tokens = kwargs.get("max_tokens")
|
|
123
|
+
if kwargs.get("max_input_tokens") is not None:
|
|
124
|
+
attributes.max_input_tokens = kwargs.get("max_input_tokens")
|
|
125
|
+
if kwargs.get("p") is not None:
|
|
126
|
+
attributes.llm_top_p = kwargs.get("p")
|
|
127
|
+
if kwargs.get("k") is not None:
|
|
128
|
+
attributes.llm_top_p = kwargs.get("k")
|
|
129
|
+
if kwargs.get("user") is not None:
|
|
130
|
+
attributes.llm_user = kwargs.get("user")
|
|
131
|
+
if kwargs.get("conversation_id") is not None:
|
|
132
|
+
attributes.conversation_id = kwargs.get("conversation_id")
|
|
133
|
+
if kwargs.get("seed") is not None:
|
|
134
|
+
attributes.seed = kwargs.get("seed")
|
|
135
|
+
if kwargs.get("frequency_penalty") is not None:
|
|
136
|
+
attributes.frequency_penalty = kwargs.get("frequency_penalty")
|
|
137
|
+
if kwargs.get("presence_penalty") is not None:
|
|
138
|
+
attributes.presence_penalty = kwargs.get("presence_penalty")
|
|
139
|
+
if kwargs.get("connectors") is not None:
|
|
140
|
+
# stringify the list of objects
|
|
141
|
+
attributes.llm_connectors = json.dumps(kwargs.get("connectors"))
|
|
142
|
+
if kwargs.get("tools") is not None:
|
|
143
|
+
# stringify the list of objects
|
|
144
|
+
attributes.llm_tools = json.dumps(kwargs.get("tools"))
|
|
145
|
+
if kwargs.get("tool_results") is not None:
|
|
146
|
+
# stringify the list of objects
|
|
147
|
+
attributes.llm_tool_results = json.dumps(kwargs.get("tool_results"))
|
|
148
|
+
|
|
149
|
+
span = tracer.start_span(
|
|
150
|
+
APIS["CHAT_CREATE"]["METHOD"], kind=SpanKind.CLIENT
|
|
151
|
+
)
|
|
152
|
+
|
|
153
|
+
# Set the attributes on the span
|
|
154
|
+
for field, value in attributes.model_dump(by_alias=True).items():
|
|
155
|
+
if value is not None:
|
|
156
|
+
span.set_attribute(field, value)
|
|
157
|
+
try:
|
|
158
|
+
# Attempt to call the original method
|
|
159
|
+
result = wrapped(*args, **kwargs)
|
|
160
|
+
|
|
161
|
+
# Set the response attributes
|
|
162
|
+
if (hasattr(result, "generation_id")) and (result.generation_id is not None):
|
|
163
|
+
span.set_attribute("llm.generation_id", result.generation_id)
|
|
164
|
+
if (hasattr(result, "response_id")) and (result.response_id is not None):
|
|
165
|
+
span.set_attribute("llm.response_id", result.response_id)
|
|
166
|
+
if (hasattr(result, "is_search_required")) and (result.is_search_required is not None):
|
|
167
|
+
span.set_attribute("llm.is_search_required", result.is_search_required)
|
|
168
|
+
|
|
169
|
+
if kwargs.get("stream") is False or kwargs.get("stream") is None:
|
|
170
|
+
if hasattr(result, "text") and result.text is not None:
|
|
171
|
+
if hasattr(result, "chat_history") and result.chat_history is not None:
|
|
172
|
+
responses = [
|
|
173
|
+
{
|
|
174
|
+
"message": {
|
|
175
|
+
"role": (
|
|
176
|
+
item.role if hasattr(item, "role") and item.role is not None else "USER"
|
|
177
|
+
),
|
|
178
|
+
"content": (
|
|
179
|
+
item.message if hasattr(item, "message") and item.message is not None else ""
|
|
180
|
+
)
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
for item in result.chat_history
|
|
184
|
+
]
|
|
185
|
+
span.set_attribute("llm.responses", json.dumps(responses))
|
|
186
|
+
else:
|
|
187
|
+
responses = [{
|
|
188
|
+
"message": {
|
|
189
|
+
"role": "CHATBOT",
|
|
190
|
+
"content": result.text
|
|
191
|
+
}
|
|
192
|
+
}]
|
|
193
|
+
span.set_attribute("llm.responses", json.dumps(responses))
|
|
194
|
+
else:
|
|
195
|
+
responses = []
|
|
196
|
+
span.set_attribute("llm.responses", json.dumps(responses))
|
|
197
|
+
|
|
198
|
+
# Get the usage
|
|
199
|
+
if hasattr(result, "meta") and result.meta is not None:
|
|
200
|
+
if hasattr(result.meta, "billed_units") and result.meta.billed_units is not None:
|
|
201
|
+
usage = result.meta.billed_units
|
|
202
|
+
if usage is not None:
|
|
203
|
+
usage_dict = {
|
|
204
|
+
"input_tokens": usage.input_tokens if usage.input_tokens is not None else 0,
|
|
205
|
+
"output_tokens": usage.output_tokens if usage.output_tokens is not None else 0,
|
|
206
|
+
"total_tokens": usage.input_tokens + usage.output_tokens if usage.input_tokens is not None and usage.output_tokens is not None else 0,
|
|
207
|
+
}
|
|
208
|
+
span.set_attribute("llm.token.counts", json.dumps(usage_dict))
|
|
209
|
+
span.set_status(StatusCode.OK)
|
|
210
|
+
span.end()
|
|
211
|
+
return result
|
|
212
|
+
else:
|
|
213
|
+
# For older version, stream was passed as a parameter
|
|
214
|
+
return result
|
|
215
|
+
|
|
216
|
+
except Exception as error:
|
|
217
|
+
span.record_exception(error)
|
|
218
|
+
span.set_status(Status(StatusCode.ERROR, str(error)))
|
|
219
|
+
span.end()
|
|
220
|
+
raise
|
|
221
|
+
return traced_method
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
def chat_stream(original_method, version, tracer):
|
|
225
|
+
"""Wrap the `messages_stream` method."""
|
|
226
|
+
|
|
227
|
+
def traced_method(wrapped, instance, args, kwargs):
|
|
228
|
+
service_provider = SERVICE_PROVIDERS["COHERE"]
|
|
229
|
+
|
|
230
|
+
message = kwargs.get("message", "")
|
|
231
|
+
prompt_tokens = estimate_tokens(message)
|
|
232
|
+
prompts = json.dumps([
|
|
233
|
+
{
|
|
234
|
+
"role": "USER",
|
|
235
|
+
"content": message
|
|
236
|
+
}
|
|
237
|
+
])
|
|
238
|
+
preamble = kwargs.get("preamble")
|
|
239
|
+
if preamble:
|
|
240
|
+
prompts = json.dumps(
|
|
241
|
+
[{"role": "system", "content": preamble}] + [{"role": "USER", "content": message}]
|
|
242
|
+
)
|
|
243
|
+
|
|
244
|
+
chat_history = kwargs.get("chat_history")
|
|
245
|
+
if chat_history:
|
|
246
|
+
history = [
|
|
247
|
+
{
|
|
248
|
+
"message": {
|
|
249
|
+
"role": (
|
|
250
|
+
item.get("role") if item.get("role") is not None else "USER"
|
|
251
|
+
),
|
|
252
|
+
"content": (
|
|
253
|
+
item.get("message") if item.get("message") is not None else ""
|
|
254
|
+
)
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
for item in chat_history
|
|
258
|
+
]
|
|
259
|
+
prompts = prompts + json.dumps(history)
|
|
260
|
+
|
|
261
|
+
extra_attributes = baggage.get_baggage(LANGTRACE_ADDITIONAL_SPAN_ATTRIBUTES_KEY)
|
|
262
|
+
|
|
263
|
+
span_attributes = {
|
|
264
|
+
"langtrace.sdk.name": "langtrace-python-sdk",
|
|
265
|
+
"langtrace.service.name": service_provider,
|
|
266
|
+
"langtrace.service.type": "llm",
|
|
267
|
+
"langtrace.service.version": version,
|
|
268
|
+
"langtrace.version": "1.0.0",
|
|
269
|
+
"url.full": APIS["CHAT_STREAM"]["URL"],
|
|
270
|
+
"llm.api": APIS["CHAT_STREAM"]["ENDPOINT"],
|
|
271
|
+
"llm.model": kwargs.get("model") if kwargs.get("model") is not None else "command-r",
|
|
272
|
+
"llm.stream": False,
|
|
273
|
+
"llm.prompts": prompts,
|
|
274
|
+
**(extra_attributes if extra_attributes is not None else {})
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
attributes = LLMSpanAttributes(**span_attributes)
|
|
278
|
+
|
|
279
|
+
if kwargs.get("temperature") is not None:
|
|
280
|
+
attributes.llm_temperature = kwargs.get("temperature")
|
|
281
|
+
if kwargs.get("max_tokens") is not None:
|
|
282
|
+
attributes.max_tokens = kwargs.get("max_tokens")
|
|
283
|
+
if kwargs.get("max_input_tokens") is not None:
|
|
284
|
+
attributes.max_input_tokens = kwargs.get("max_input_tokens")
|
|
285
|
+
if kwargs.get("p") is not None:
|
|
286
|
+
attributes.llm_top_p = kwargs.get("p")
|
|
287
|
+
if kwargs.get("k") is not None:
|
|
288
|
+
attributes.llm_top_p = kwargs.get("k")
|
|
289
|
+
if kwargs.get("user") is not None:
|
|
290
|
+
attributes.llm_user = kwargs.get("user")
|
|
291
|
+
if kwargs.get("conversation_id") is not None:
|
|
292
|
+
attributes.conversation_id = kwargs.get("conversation_id")
|
|
293
|
+
if kwargs.get("seed") is not None:
|
|
294
|
+
attributes.seed = kwargs.get("seed")
|
|
295
|
+
if kwargs.get("frequency_penalty") is not None:
|
|
296
|
+
attributes.frequency_penalty = kwargs.get("frequency_penalty")
|
|
297
|
+
if kwargs.get("presence_penalty") is not None:
|
|
298
|
+
attributes.presence_penalty = kwargs.get("presence_penalty")
|
|
299
|
+
if kwargs.get("connectors") is not None:
|
|
300
|
+
# stringify the list of objects
|
|
301
|
+
attributes.llm_connectors = json.dumps(kwargs.get("connectors"))
|
|
302
|
+
if kwargs.get("tools") is not None:
|
|
303
|
+
# stringify the list of objects
|
|
304
|
+
attributes.llm_tools = json.dumps(kwargs.get("tools"))
|
|
305
|
+
if kwargs.get("tool_results") is not None:
|
|
306
|
+
# stringify the list of objects
|
|
307
|
+
attributes.llm_tool_results = json.dumps(kwargs.get("tool_results"))
|
|
308
|
+
|
|
309
|
+
span = tracer.start_span(
|
|
310
|
+
APIS["CHAT_CREATE"]["METHOD"], kind=SpanKind.CLIENT
|
|
311
|
+
)
|
|
312
|
+
for field, value in attributes.model_dump(by_alias=True).items():
|
|
313
|
+
if value is not None:
|
|
314
|
+
span.set_attribute(field, value)
|
|
315
|
+
try:
|
|
316
|
+
# Attempt to call the original method
|
|
317
|
+
result = wrapped(*args, **kwargs)
|
|
318
|
+
|
|
319
|
+
result_content = []
|
|
320
|
+
span.add_event(Event.STREAM_START.value)
|
|
321
|
+
completion_tokens = 0
|
|
322
|
+
try:
|
|
323
|
+
for event in result:
|
|
324
|
+
if hasattr(event, "text") and event.text is not None:
|
|
325
|
+
completion_tokens += estimate_tokens(event.text)
|
|
326
|
+
content = event.text
|
|
327
|
+
else:
|
|
328
|
+
content = ""
|
|
329
|
+
span.add_event(
|
|
330
|
+
Event.STREAM_OUTPUT.value, {"response": "".join(content)}
|
|
331
|
+
)
|
|
332
|
+
result_content.append(content)
|
|
333
|
+
yield event
|
|
334
|
+
finally:
|
|
335
|
+
|
|
336
|
+
# Finalize span after processing all chunks
|
|
337
|
+
span.add_event(Event.STREAM_END.value)
|
|
338
|
+
span.set_attribute(
|
|
339
|
+
"llm.token.counts",
|
|
340
|
+
json.dumps(
|
|
341
|
+
{
|
|
342
|
+
"input_tokens": prompt_tokens,
|
|
343
|
+
"output_tokens": completion_tokens,
|
|
344
|
+
"total_tokens": prompt_tokens + completion_tokens,
|
|
345
|
+
}
|
|
346
|
+
),
|
|
347
|
+
)
|
|
348
|
+
span.set_attribute(
|
|
349
|
+
"llm.responses",
|
|
350
|
+
json.dumps(
|
|
351
|
+
[
|
|
352
|
+
{
|
|
353
|
+
"message": {
|
|
354
|
+
"role": "CHATBOT",
|
|
355
|
+
"content": "".join(result_content),
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
]
|
|
359
|
+
),
|
|
360
|
+
)
|
|
361
|
+
span.set_status(StatusCode.OK)
|
|
362
|
+
span.end()
|
|
363
|
+
|
|
364
|
+
except Exception as error:
|
|
365
|
+
span.record_exception(error)
|
|
366
|
+
span.set_status(Status(StatusCode.ERROR, str(error)))
|
|
367
|
+
span.end()
|
|
368
|
+
raise
|
|
369
|
+
|
|
370
|
+
return traced_method
|
|
@@ -220,12 +220,10 @@ def chat_completions_create(original_method, version, tracer):
|
|
|
220
220
|
prompt_tokens,
|
|
221
221
|
function_call=kwargs.get("functions") is not None,
|
|
222
222
|
)
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
span.record_exception(
|
|
226
|
-
|
|
227
|
-
span.set_status(Status(StatusCode.ERROR, str(e)))
|
|
228
|
-
# Reraise the exception to ensure it's not swallowed
|
|
223
|
+
|
|
224
|
+
except Exception as error:
|
|
225
|
+
span.record_exception(error)
|
|
226
|
+
span.set_status(Status(StatusCode.ERROR, str(error)))
|
|
229
227
|
span.end()
|
|
230
228
|
raise
|
|
231
229
|
|
|
@@ -32,6 +32,9 @@ from langtrace_python_sdk.instrumentation.openai.instrumentation import (
|
|
|
32
32
|
from langtrace_python_sdk.instrumentation.pinecone.instrumentation import (
|
|
33
33
|
PineconeInstrumentation,
|
|
34
34
|
)
|
|
35
|
+
from langtrace_python_sdk.instrumentation.cohere.instrumentation import (
|
|
36
|
+
CohereInstrumentation,
|
|
37
|
+
)
|
|
35
38
|
|
|
36
39
|
|
|
37
40
|
def init(
|
|
@@ -77,6 +80,7 @@ def init(
|
|
|
77
80
|
langchain_core_instrumentation = LangchainCoreInstrumentation()
|
|
78
81
|
langchain_community_instrumentation = LangchainCommunityInstrumentation()
|
|
79
82
|
anthropic_instrumentation = AnthropicInstrumentation()
|
|
83
|
+
cohere_instrumentation = CohereInstrumentation()
|
|
80
84
|
|
|
81
85
|
# Call the instrument method with some arguments
|
|
82
86
|
openai_instrumentation.instrument()
|
|
@@ -87,3 +91,4 @@ def init(
|
|
|
87
91
|
langchain_core_instrumentation.instrument()
|
|
88
92
|
langchain_community_instrumentation.instrument()
|
|
89
93
|
anthropic_instrumentation.instrument()
|
|
94
|
+
cohere_instrumentation.instrument()
|
langtrace_python_sdk/version.py
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
__version__ = "1.
|
|
1
|
+
__version__ = "1.3.1"
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.3
|
|
2
2
|
Name: langtrace-python-sdk
|
|
3
|
-
Version: 1.
|
|
3
|
+
Version: 1.3.1
|
|
4
4
|
Summary: Python SDK for LangTrace
|
|
5
5
|
Project-URL: Homepage, https://github.com/Scale3-Labs/langtrace-python-sdk
|
|
6
6
|
Author-email: Scale3 Labs <engineering@scale3labs.com>
|
|
@@ -220,6 +220,7 @@ Langtrace automatically captures traces from the following vendors:
|
|
|
220
220
|
| ------ | ------ | ------ | ------ |
|
|
221
221
|
| OpenAI | LLM | :white_check_mark: | :white_check_mark: |
|
|
222
222
|
| Anthropic | LLM | :white_check_mark: | :white_check_mark: |
|
|
223
|
+
| Cohere | LLM | :x: | :white_check_mark: |
|
|
223
224
|
| Azure OpenAI | LLM | :white_check_mark: | :white_check_mark: |
|
|
224
225
|
| Langchain | Framework | :x: | :white_check_mark: |
|
|
225
226
|
| LlamaIndex | Framework | :white_check_mark: | :white_check_mark: |
|
|
@@ -3,6 +3,11 @@ examples/anthropic_example/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJ
|
|
|
3
3
|
examples/anthropic_example/completion.py,sha256=97CA6u7iLdTj6qQ5XA7nB0bcNMxUwXJKpNYSCW-BM6Q,791
|
|
4
4
|
examples/chroma_example/__init__.py,sha256=Tq6pae7fHA7dwuDslabB5MTNedL21gu2RaZicpxSyLU,25
|
|
5
5
|
examples/chroma_example/basic.py,sha256=VE-I7Db58Dyb0G6WgX9FeloHLGuRRvfB4aNvSIVL4Uk,910
|
|
6
|
+
examples/cohere_example/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
7
|
+
examples/cohere_example/chat.py,sha256=MpTyvXPNTMcjKzQUsF4xepwTxBNcDbUYmjawzjZIIo4,889
|
|
8
|
+
examples/cohere_example/chat_stream.py,sha256=t9XmyC_sT3ZdDQKzECPcX-8hpyYzrL6OXihsAKmPxTA,680
|
|
9
|
+
examples/cohere_example/embed_create.py,sha256=JYLCfnES4TX-RbU6n3wIlLEfr5pfCwOwdRopjvxytew,552
|
|
10
|
+
examples/fastapi_example/basic_route.py,sha256=3wu1cogsR9eeuwid9kRBepVxMUZ0np-T5qkmsvQh0Lw,1145
|
|
6
11
|
examples/langchain_example/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
7
12
|
examples/langchain_example/basic.py,sha256=mSVcE-Kx7jNS4U_zrm5WZI98xyw6WTrhd2GMVGgn9Cg,2653
|
|
8
13
|
examples/langchain_example/tool.py,sha256=8T8_IDbgA58XbsfyH5_xhA8ZKQfyfyFxF8wor-PsRjA,2556
|
|
@@ -18,14 +23,15 @@ examples/perplexity_example/basic.py,sha256=jEqZnY9cmDezMAwMvKaZeRk7vW1H68Zc2de3
|
|
|
18
23
|
examples/pinecone_example/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
19
24
|
examples/pinecone_example/basic.py,sha256=hdV6-5Fmol9zeyFzDtdabD62vkqUJ4lCHG2YcVNpIpI,933
|
|
20
25
|
langtrace_python_sdk/__init__.py,sha256=SlHg447-nQBbw8exRNJP_OyHUZ39Sldb7aaQ35hIRm8,262
|
|
21
|
-
langtrace_python_sdk/langtrace.py,sha256
|
|
22
|
-
langtrace_python_sdk/version.py,sha256
|
|
26
|
+
langtrace_python_sdk/langtrace.py,sha256=-9WC49AOmxmEWl_fUXkEAKgshg3pmI5J-_d_1hITjY8,3447
|
|
27
|
+
langtrace_python_sdk/version.py,sha256=-ypEJktJToAL9by62JJKWEzDo_KPCQtmE5kwFgX24z4,22
|
|
23
28
|
langtrace_python_sdk/constants/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
24
29
|
langtrace_python_sdk/constants/exporter/langtrace_exporter.py,sha256=5MNjnAOg-4am78J3gVMH6FSwq5N8TOj72ugkhsw4vi0,46
|
|
25
30
|
langtrace_python_sdk/constants/instrumentation/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
26
31
|
langtrace_python_sdk/constants/instrumentation/anthropic.py,sha256=YX3llt3zwDY6XrYk3CB8WEVqgrzRXEw_ffyk56JoF3k,126
|
|
27
32
|
langtrace_python_sdk/constants/instrumentation/chroma.py,sha256=hiPGYdHS0Yj4Kh3eaYBbuCAl_swqIygu80yFqkOgdak,955
|
|
28
|
-
langtrace_python_sdk/constants/instrumentation/
|
|
33
|
+
langtrace_python_sdk/constants/instrumentation/cohere.py,sha256=jB6PsLmvoRAIjpbuv9j-59_VRL0OW3w5BiB54a72jBc,445
|
|
34
|
+
langtrace_python_sdk/constants/instrumentation/common.py,sha256=iqoWV1laoFxzqqwzNmkCrbyVo64n3nrwSoY_r57Sx3g,652
|
|
29
35
|
langtrace_python_sdk/constants/instrumentation/openai.py,sha256=9VF6ic9Ed3bpSvdp6iNmrpx2Ppo6DPav3hoUcqSQSv0,1048
|
|
30
36
|
langtrace_python_sdk/constants/instrumentation/pinecone.py,sha256=Xaqqw-xBO0JJLGk75hiCUQGztNm0HiVaLQvjtYK7VJM,472
|
|
31
37
|
langtrace_python_sdk/extensions/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
@@ -37,6 +43,9 @@ langtrace_python_sdk/instrumentation/anthropic/patch.py,sha256=BovObJMmux-VbumON
|
|
|
37
43
|
langtrace_python_sdk/instrumentation/chroma/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
38
44
|
langtrace_python_sdk/instrumentation/chroma/instrumentation.py,sha256=9k8KueizRJuicJnuYmSvjLwdeCNszZ1KpwZPMsQ_7ig,1280
|
|
39
45
|
langtrace_python_sdk/instrumentation/chroma/patch.py,sha256=fddeGMP8obll2Tra04S8C0jEiJ97v_y1WC1QdO4bO4A,2303
|
|
46
|
+
langtrace_python_sdk/instrumentation/cohere/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
47
|
+
langtrace_python_sdk/instrumentation/cohere/instrumentation.py,sha256=-DWjAq4_H3Q8MCyH2qt3NiLndgrRy9BIUbrZZHsY0pY,1431
|
|
48
|
+
langtrace_python_sdk/instrumentation/cohere/patch.py,sha256=PpDEmWc3XFnrFOvyytluaJN25TQqE4ZOR0WZMYqzg2E,15727
|
|
40
49
|
langtrace_python_sdk/instrumentation/langchain/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
41
50
|
langtrace_python_sdk/instrumentation/langchain/instrumentation.py,sha256=UBUx31IErih1zDGkwez5ZFT2qGvCHxhdGHvJsEyhvEo,2938
|
|
42
51
|
langtrace_python_sdk/instrumentation/langchain/patch.py,sha256=ofageMjQZ_AGlnfknBI06dkaDsyNuqYRz-3EZhv9izY,3294
|
|
@@ -51,14 +60,14 @@ langtrace_python_sdk/instrumentation/llamaindex/instrumentation.py,sha256=wVSvxS
|
|
|
51
60
|
langtrace_python_sdk/instrumentation/llamaindex/patch.py,sha256=NqBsPM4eE3yMu1aIev8p8yH5Thsv72JL68ABEZNe2ug,2043
|
|
52
61
|
langtrace_python_sdk/instrumentation/openai/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
53
62
|
langtrace_python_sdk/instrumentation/openai/instrumentation.py,sha256=stlFopPggB8mgnn4QdVh41rmA8mlzH5x-C00XHfApCE,1408
|
|
54
|
-
langtrace_python_sdk/instrumentation/openai/patch.py,sha256=
|
|
63
|
+
langtrace_python_sdk/instrumentation/openai/patch.py,sha256=fTyZudq7hjk-BEZad4ayX6SaCCF0053_mSmeC43Lu-c,15463
|
|
55
64
|
langtrace_python_sdk/instrumentation/pinecone/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
56
65
|
langtrace_python_sdk/instrumentation/pinecone/instrumentation.py,sha256=o0EUd5jvHaDKOUTj4NjnL5UfDHDHxyXkWGlTW4oeRDk,1784
|
|
57
66
|
langtrace_python_sdk/instrumentation/pinecone/patch.py,sha256=5lF7hQmg2-U2EWtOC0w8_peRaNMysBomb0fjiNoS6eQ,2200
|
|
58
67
|
langtrace_python_sdk/utils/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
59
68
|
langtrace_python_sdk/utils/llm.py,sha256=4z2e-md_ELXCEuOIRVWracR6qH2pmsOxCqpkuF9_3Nw,1589
|
|
60
69
|
langtrace_python_sdk/utils/with_root_span.py,sha256=N7ONrcF0myZbHBy5gpQffDbX-Kf63Crsz9szG0i3m08,1889
|
|
61
|
-
langtrace_python_sdk-1.
|
|
62
|
-
langtrace_python_sdk-1.
|
|
63
|
-
langtrace_python_sdk-1.
|
|
64
|
-
langtrace_python_sdk-1.
|
|
70
|
+
langtrace_python_sdk-1.3.1.dist-info/METADATA,sha256=eTyOCa7I4N386Gi0evo8yefecH0ntGFMK2nSzihOVfg,9061
|
|
71
|
+
langtrace_python_sdk-1.3.1.dist-info/WHEEL,sha256=as-1oFTWSeWBgyzh0O_qF439xqBe6AbBgt4MfYe5zwY,87
|
|
72
|
+
langtrace_python_sdk-1.3.1.dist-info/licenses/LICENSE,sha256=QwcOLU5TJoTeUhuIXzhdCEEDDvorGiC6-3YTOl4TecE,11356
|
|
73
|
+
langtrace_python_sdk-1.3.1.dist-info/RECORD,,
|
|
File without changes
|
{langtrace_python_sdk-1.2.25.dist-info → langtrace_python_sdk-1.3.1.dist-info}/licenses/LICENSE
RENAMED
|
File without changes
|