langtrace-python-sdk 1.2.23__tar.gz → 1.3.1__tar.gz

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.
Files changed (87) hide show
  1. {langtrace_python_sdk-1.2.23 → langtrace_python_sdk-1.3.1}/PKG-INFO +64 -1
  2. {langtrace_python_sdk-1.2.23 → langtrace_python_sdk-1.3.1}/README.md +63 -0
  3. langtrace_python_sdk-1.3.1/src/examples/cohere_example/chat.py +26 -0
  4. langtrace_python_sdk-1.3.1/src/examples/cohere_example/chat_stream.py +23 -0
  5. langtrace_python_sdk-1.3.1/src/examples/cohere_example/embed_create.py +21 -0
  6. langtrace_python_sdk-1.3.1/src/examples/fastapi_example/basic_route.py +38 -0
  7. langtrace_python_sdk-1.3.1/src/examples/perplexity_example/basic.py +21 -0
  8. langtrace_python_sdk-1.3.1/src/langtrace_python_sdk/constants/instrumentation/cohere.py +17 -0
  9. {langtrace_python_sdk-1.2.23 → langtrace_python_sdk-1.3.1}/src/langtrace_python_sdk/constants/instrumentation/common.py +2 -0
  10. langtrace_python_sdk-1.3.1/src/langtrace_python_sdk/instrumentation/cohere/instrumentation.py +53 -0
  11. langtrace_python_sdk-1.3.1/src/langtrace_python_sdk/instrumentation/cohere/patch.py +370 -0
  12. {langtrace_python_sdk-1.2.23 → langtrace_python_sdk-1.3.1}/src/langtrace_python_sdk/instrumentation/openai/patch.py +10 -6
  13. {langtrace_python_sdk-1.2.23 → langtrace_python_sdk-1.3.1}/src/langtrace_python_sdk/langtrace.py +5 -0
  14. langtrace_python_sdk-1.3.1/src/langtrace_python_sdk/utils/__init__.py +0 -0
  15. langtrace_python_sdk-1.3.1/src/langtrace_python_sdk/version.py +1 -0
  16. {langtrace_python_sdk-1.2.23 → langtrace_python_sdk-1.3.1}/src/run_example.py +10 -2
  17. langtrace_python_sdk-1.3.1/src/tests/__init__.py +0 -0
  18. langtrace_python_sdk-1.2.23/src/langtrace_python_sdk/version.py +0 -1
  19. {langtrace_python_sdk-1.2.23 → langtrace_python_sdk-1.3.1}/.gitignore +0 -0
  20. {langtrace_python_sdk-1.2.23 → langtrace_python_sdk-1.3.1}/LICENSE +0 -0
  21. {langtrace_python_sdk-1.2.23 → langtrace_python_sdk-1.3.1}/pyproject.toml +0 -0
  22. {langtrace_python_sdk-1.2.23 → langtrace_python_sdk-1.3.1}/src/__init__.py +0 -0
  23. {langtrace_python_sdk-1.2.23 → langtrace_python_sdk-1.3.1}/src/examples/__init__.py +0 -0
  24. {langtrace_python_sdk-1.2.23 → langtrace_python_sdk-1.3.1}/src/examples/anthropic_example/__init__.py +0 -0
  25. {langtrace_python_sdk-1.2.23 → langtrace_python_sdk-1.3.1}/src/examples/anthropic_example/completion.py +0 -0
  26. {langtrace_python_sdk-1.2.23 → langtrace_python_sdk-1.3.1}/src/examples/chroma_example/__init__.py +0 -0
  27. {langtrace_python_sdk-1.2.23 → langtrace_python_sdk-1.3.1}/src/examples/chroma_example/basic.py +0 -0
  28. {langtrace_python_sdk-1.2.23/src/examples/langchain_example → langtrace_python_sdk-1.3.1/src/examples/cohere_example}/__init__.py +0 -0
  29. {langtrace_python_sdk-1.2.23/src/examples/llamaindex_example → langtrace_python_sdk-1.3.1/src/examples/langchain_example}/__init__.py +0 -0
  30. {langtrace_python_sdk-1.2.23 → langtrace_python_sdk-1.3.1}/src/examples/langchain_example/basic.py +0 -0
  31. {langtrace_python_sdk-1.2.23 → langtrace_python_sdk-1.3.1}/src/examples/langchain_example/tool.py +0 -0
  32. {langtrace_python_sdk-1.2.23/src/examples/openai → langtrace_python_sdk-1.3.1/src/examples/llamaindex_example}/__init__.py +0 -0
  33. {langtrace_python_sdk-1.2.23 → langtrace_python_sdk-1.3.1}/src/examples/llamaindex_example/basic.py +0 -0
  34. {langtrace_python_sdk-1.2.23 → langtrace_python_sdk-1.3.1}/src/examples/llamaindex_example/data/abramov.txt +0 -0
  35. {langtrace_python_sdk-1.2.23/src/examples/pinecone_example → langtrace_python_sdk-1.3.1/src/examples/openai}/__init__.py +0 -0
  36. {langtrace_python_sdk-1.2.23 → langtrace_python_sdk-1.3.1}/src/examples/openai/chat_completion.py +0 -0
  37. {langtrace_python_sdk-1.2.23 → langtrace_python_sdk-1.3.1}/src/examples/openai/embeddings_create.py +0 -0
  38. {langtrace_python_sdk-1.2.23 → langtrace_python_sdk-1.3.1}/src/examples/openai/function_calling.py +0 -0
  39. {langtrace_python_sdk-1.2.23 → langtrace_python_sdk-1.3.1}/src/examples/openai/images_generate.py +0 -0
  40. {langtrace_python_sdk-1.2.23/src/langtrace_python_sdk/constants → langtrace_python_sdk-1.3.1/src/examples/pinecone_example}/__init__.py +0 -0
  41. {langtrace_python_sdk-1.2.23 → langtrace_python_sdk-1.3.1}/src/examples/pinecone_example/basic.py +0 -0
  42. {langtrace_python_sdk-1.2.23 → langtrace_python_sdk-1.3.1}/src/langtrace_python_sdk/__init__.py +0 -0
  43. {langtrace_python_sdk-1.2.23/src/langtrace_python_sdk/constants/instrumentation → langtrace_python_sdk-1.3.1/src/langtrace_python_sdk/constants}/__init__.py +0 -0
  44. {langtrace_python_sdk-1.2.23 → langtrace_python_sdk-1.3.1}/src/langtrace_python_sdk/constants/exporter/langtrace_exporter.py +0 -0
  45. {langtrace_python_sdk-1.2.23/src/langtrace_python_sdk/extensions → langtrace_python_sdk-1.3.1/src/langtrace_python_sdk/constants/instrumentation}/__init__.py +0 -0
  46. {langtrace_python_sdk-1.2.23 → langtrace_python_sdk-1.3.1}/src/langtrace_python_sdk/constants/instrumentation/anthropic.py +0 -0
  47. {langtrace_python_sdk-1.2.23 → langtrace_python_sdk-1.3.1}/src/langtrace_python_sdk/constants/instrumentation/chroma.py +0 -0
  48. {langtrace_python_sdk-1.2.23 → langtrace_python_sdk-1.3.1}/src/langtrace_python_sdk/constants/instrumentation/openai.py +0 -0
  49. {langtrace_python_sdk-1.2.23 → langtrace_python_sdk-1.3.1}/src/langtrace_python_sdk/constants/instrumentation/pinecone.py +0 -0
  50. {langtrace_python_sdk-1.2.23/src/langtrace_python_sdk/instrumentation → langtrace_python_sdk-1.3.1/src/langtrace_python_sdk/extensions}/__init__.py +0 -0
  51. {langtrace_python_sdk-1.2.23 → langtrace_python_sdk-1.3.1}/src/langtrace_python_sdk/extensions/langtrace_exporter.py +0 -0
  52. {langtrace_python_sdk-1.2.23/src/langtrace_python_sdk/instrumentation/anthropic → langtrace_python_sdk-1.3.1/src/langtrace_python_sdk/instrumentation}/__init__.py +0 -0
  53. {langtrace_python_sdk-1.2.23/src/langtrace_python_sdk/instrumentation/chroma → langtrace_python_sdk-1.3.1/src/langtrace_python_sdk/instrumentation/anthropic}/__init__.py +0 -0
  54. {langtrace_python_sdk-1.2.23 → langtrace_python_sdk-1.3.1}/src/langtrace_python_sdk/instrumentation/anthropic/instrumentation.py +0 -0
  55. {langtrace_python_sdk-1.2.23 → langtrace_python_sdk-1.3.1}/src/langtrace_python_sdk/instrumentation/anthropic/patch.py +0 -0
  56. {langtrace_python_sdk-1.2.23/src/langtrace_python_sdk/instrumentation/langchain → langtrace_python_sdk-1.3.1/src/langtrace_python_sdk/instrumentation/chroma}/__init__.py +0 -0
  57. {langtrace_python_sdk-1.2.23 → langtrace_python_sdk-1.3.1}/src/langtrace_python_sdk/instrumentation/chroma/instrumentation.py +0 -0
  58. {langtrace_python_sdk-1.2.23 → langtrace_python_sdk-1.3.1}/src/langtrace_python_sdk/instrumentation/chroma/patch.py +0 -0
  59. {langtrace_python_sdk-1.2.23/src/langtrace_python_sdk/instrumentation/langchain_community → langtrace_python_sdk-1.3.1/src/langtrace_python_sdk/instrumentation/cohere}/__init__.py +0 -0
  60. {langtrace_python_sdk-1.2.23/src/langtrace_python_sdk/instrumentation/langchain_core → langtrace_python_sdk-1.3.1/src/langtrace_python_sdk/instrumentation/langchain}/__init__.py +0 -0
  61. {langtrace_python_sdk-1.2.23 → langtrace_python_sdk-1.3.1}/src/langtrace_python_sdk/instrumentation/langchain/instrumentation.py +0 -0
  62. {langtrace_python_sdk-1.2.23 → langtrace_python_sdk-1.3.1}/src/langtrace_python_sdk/instrumentation/langchain/patch.py +0 -0
  63. {langtrace_python_sdk-1.2.23/src/langtrace_python_sdk/instrumentation/llamaindex → langtrace_python_sdk-1.3.1/src/langtrace_python_sdk/instrumentation/langchain_community}/__init__.py +0 -0
  64. {langtrace_python_sdk-1.2.23 → langtrace_python_sdk-1.3.1}/src/langtrace_python_sdk/instrumentation/langchain_community/instrumentation.py +0 -0
  65. {langtrace_python_sdk-1.2.23 → langtrace_python_sdk-1.3.1}/src/langtrace_python_sdk/instrumentation/langchain_community/patch.py +0 -0
  66. {langtrace_python_sdk-1.2.23/src/langtrace_python_sdk/instrumentation/openai → langtrace_python_sdk-1.3.1/src/langtrace_python_sdk/instrumentation/langchain_core}/__init__.py +0 -0
  67. {langtrace_python_sdk-1.2.23 → langtrace_python_sdk-1.3.1}/src/langtrace_python_sdk/instrumentation/langchain_core/instrumentation.py +0 -0
  68. {langtrace_python_sdk-1.2.23 → langtrace_python_sdk-1.3.1}/src/langtrace_python_sdk/instrumentation/langchain_core/patch.py +0 -0
  69. {langtrace_python_sdk-1.2.23/src/langtrace_python_sdk/instrumentation/pinecone → langtrace_python_sdk-1.3.1/src/langtrace_python_sdk/instrumentation/llamaindex}/__init__.py +0 -0
  70. {langtrace_python_sdk-1.2.23 → langtrace_python_sdk-1.3.1}/src/langtrace_python_sdk/instrumentation/llamaindex/instrumentation.py +0 -0
  71. {langtrace_python_sdk-1.2.23 → langtrace_python_sdk-1.3.1}/src/langtrace_python_sdk/instrumentation/llamaindex/patch.py +0 -0
  72. {langtrace_python_sdk-1.2.23/src/langtrace_python_sdk/utils → langtrace_python_sdk-1.3.1/src/langtrace_python_sdk/instrumentation/openai}/__init__.py +0 -0
  73. {langtrace_python_sdk-1.2.23 → langtrace_python_sdk-1.3.1}/src/langtrace_python_sdk/instrumentation/openai/instrumentation.py +0 -0
  74. {langtrace_python_sdk-1.2.23/src/tests → langtrace_python_sdk-1.3.1/src/langtrace_python_sdk/instrumentation/pinecone}/__init__.py +0 -0
  75. {langtrace_python_sdk-1.2.23 → langtrace_python_sdk-1.3.1}/src/langtrace_python_sdk/instrumentation/pinecone/instrumentation.py +0 -0
  76. {langtrace_python_sdk-1.2.23 → langtrace_python_sdk-1.3.1}/src/langtrace_python_sdk/instrumentation/pinecone/patch.py +0 -0
  77. {langtrace_python_sdk-1.2.23 → langtrace_python_sdk-1.3.1}/src/langtrace_python_sdk/utils/llm.py +0 -0
  78. {langtrace_python_sdk-1.2.23 → langtrace_python_sdk-1.3.1}/src/langtrace_python_sdk/utils/with_root_span.py +0 -0
  79. {langtrace_python_sdk-1.2.23 → langtrace_python_sdk-1.3.1}/src/tests/anthropic/test_anthropic.py +0 -0
  80. {langtrace_python_sdk-1.2.23 → langtrace_python_sdk-1.3.1}/src/tests/chroma/test_chroma.py +0 -0
  81. {langtrace_python_sdk-1.2.23 → langtrace_python_sdk-1.3.1}/src/tests/langchain/test_langchain.py +0 -0
  82. {langtrace_python_sdk-1.2.23 → langtrace_python_sdk-1.3.1}/src/tests/langchain/test_langchain_community.py +0 -0
  83. {langtrace_python_sdk-1.2.23 → langtrace_python_sdk-1.3.1}/src/tests/langchain/test_langchain_core.py +0 -0
  84. {langtrace_python_sdk-1.2.23 → langtrace_python_sdk-1.3.1}/src/tests/openai/test_chat_completion.py +0 -0
  85. {langtrace_python_sdk-1.2.23 → langtrace_python_sdk-1.3.1}/src/tests/openai/test_image_generation.py +0 -0
  86. {langtrace_python_sdk-1.2.23 → langtrace_python_sdk-1.3.1}/src/tests/pinecone/test_pinecone.py +0 -0
  87. {langtrace_python_sdk-1.2.23 → langtrace_python_sdk-1.3.1}/src/tests/utils.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: langtrace-python-sdk
3
- Version: 1.2.23
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>
@@ -72,6 +72,68 @@ from langtrace_python_sdk import langtrace # Must precede any llm module imports
72
72
  langtrace.init() # LANGTRACE_API_KEY as an ENVIRONMENT variable
73
73
  ```
74
74
 
75
+ ## FastAPI Quick Start
76
+ Initialize FastAPI project and add this inside the ```main.py``` file
77
+
78
+ ```python
79
+ from fastapi import FastAPI
80
+ from langtrace_python_sdk import langtrace
81
+ from openai import OpenAI
82
+
83
+ langtrace.init()
84
+ app = FastAPI()
85
+ client = OpenAI()
86
+
87
+ @app.get("/")
88
+ def root():
89
+ client.chat.completions.create(
90
+ model="gpt-4",
91
+ messages=[{"role": "user", "content": "Say this is a test three times"}],
92
+ stream=False,
93
+ )
94
+ return {"Hello": "World"}
95
+ ```
96
+
97
+ ## Django Quick Start
98
+ Initialize django project and add this inside the ```__init.py__``` file
99
+ ```python
100
+ from langtrace_python_sdk import langtrace
101
+ from openai import OpenAI
102
+
103
+
104
+ langtrace.init()
105
+ client = OpenAI()
106
+
107
+ client.chat.completions.create(
108
+ model="gpt-4",
109
+ messages=[{"role": "user", "content": "Say this is a test three times"}],
110
+ stream=False,
111
+ )
112
+
113
+ ```
114
+
115
+ ## Flask Quick Start
116
+ Initialize flask project and this inside ```app.py``` file
117
+ ```python
118
+ from flask import Flask
119
+ from langtrace_python_sdk import langtrace
120
+ from openai import OpenAI
121
+
122
+ langtrace.init()
123
+ client = OpenAI()
124
+ app = Flask(__name__)
125
+
126
+
127
+ @app.route("/")
128
+ def main():
129
+ client.chat.completions.create(
130
+ model="gpt-4",
131
+ messages=[{"role": "user", "content": "Say this is a test three times"}],
132
+ stream=False,
133
+ )
134
+ return "Hello, World!"
135
+ ```
136
+
75
137
  ## Langtrace Self Hosted
76
138
 
77
139
  Get started by adding simply two lines to your code and see traces being logged to the console!
@@ -158,6 +220,7 @@ Langtrace automatically captures traces from the following vendors:
158
220
  | ------ | ------ | ------ | ------ |
159
221
  | OpenAI | LLM | :white_check_mark: | :white_check_mark: |
160
222
  | Anthropic | LLM | :white_check_mark: | :white_check_mark: |
223
+ | Cohere | LLM | :x: | :white_check_mark: |
161
224
  | Azure OpenAI | LLM | :white_check_mark: | :white_check_mark: |
162
225
  | Langchain | Framework | :x: | :white_check_mark: |
163
226
  | LlamaIndex | Framework | :white_check_mark: | :white_check_mark: |
@@ -44,6 +44,68 @@ from langtrace_python_sdk import langtrace # Must precede any llm module imports
44
44
  langtrace.init() # LANGTRACE_API_KEY as an ENVIRONMENT variable
45
45
  ```
46
46
 
47
+ ## FastAPI Quick Start
48
+ Initialize FastAPI project and add this inside the ```main.py``` file
49
+
50
+ ```python
51
+ from fastapi import FastAPI
52
+ from langtrace_python_sdk import langtrace
53
+ from openai import OpenAI
54
+
55
+ langtrace.init()
56
+ app = FastAPI()
57
+ client = OpenAI()
58
+
59
+ @app.get("/")
60
+ def root():
61
+ client.chat.completions.create(
62
+ model="gpt-4",
63
+ messages=[{"role": "user", "content": "Say this is a test three times"}],
64
+ stream=False,
65
+ )
66
+ return {"Hello": "World"}
67
+ ```
68
+
69
+ ## Django Quick Start
70
+ Initialize django project and add this inside the ```__init.py__``` file
71
+ ```python
72
+ from langtrace_python_sdk import langtrace
73
+ from openai import OpenAI
74
+
75
+
76
+ langtrace.init()
77
+ client = OpenAI()
78
+
79
+ client.chat.completions.create(
80
+ model="gpt-4",
81
+ messages=[{"role": "user", "content": "Say this is a test three times"}],
82
+ stream=False,
83
+ )
84
+
85
+ ```
86
+
87
+ ## Flask Quick Start
88
+ Initialize flask project and this inside ```app.py``` file
89
+ ```python
90
+ from flask import Flask
91
+ from langtrace_python_sdk import langtrace
92
+ from openai import OpenAI
93
+
94
+ langtrace.init()
95
+ client = OpenAI()
96
+ app = Flask(__name__)
97
+
98
+
99
+ @app.route("/")
100
+ def main():
101
+ client.chat.completions.create(
102
+ model="gpt-4",
103
+ messages=[{"role": "user", "content": "Say this is a test three times"}],
104
+ stream=False,
105
+ )
106
+ return "Hello, World!"
107
+ ```
108
+
47
109
  ## Langtrace Self Hosted
48
110
 
49
111
  Get started by adding simply two lines to your code and see traces being logged to the console!
@@ -130,6 +192,7 @@ Langtrace automatically captures traces from the following vendors:
130
192
  | ------ | ------ | ------ | ------ |
131
193
  | OpenAI | LLM | :white_check_mark: | :white_check_mark: |
132
194
  | Anthropic | LLM | :white_check_mark: | :white_check_mark: |
195
+ | Cohere | LLM | :x: | :white_check_mark: |
133
196
  | Azure OpenAI | LLM | :white_check_mark: | :white_check_mark: |
134
197
  | Langchain | Framework | :x: | :white_check_mark: |
135
198
  | LlamaIndex | Framework | :white_check_mark: | :white_check_mark: |
@@ -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,21 @@
1
+ from openai import OpenAI
2
+
3
+ from langtrace_python_sdk import langtrace
4
+ from langtrace_python_sdk.utils.with_root_span import (
5
+ with_additional_attributes, with_langtrace_root_span)
6
+
7
+ # _ = load_dotenv(find_dotenv())
8
+
9
+ langtrace.init(batch=False, write_to_langtrace_cloud=False, debug_log_to_console=True)
10
+ client = OpenAI(base_url="https://api.perplexity.ai", api_key="PPLX_API_KEY")
11
+
12
+
13
+ @with_additional_attributes({"user.id": "1234", "user.feedback.rating": 1})
14
+ def basic():
15
+ response = client.chat.completions.create(
16
+ model="pplx-70b-online",
17
+ messages=[{"role": "user", "content": "What is the capital of France?"}],
18
+ stream=False,
19
+ )
20
+ print(response)
21
+ return response
@@ -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
+ }
@@ -16,6 +16,8 @@ SERVICE_PROVIDERS = {
16
16
  "LLAMAINDEX": "LlamaIndex",
17
17
  "OPENAI": "OpenAI",
18
18
  "PINECONE": "Pinecone",
19
+ "COHERE": "Cohere",
20
+ "PPLX": "Perplexity",
19
21
  }
20
22
 
21
23
  LANGTRACE_ADDITIONAL_SPAN_ATTRIBUTES_KEY = "langtrace_additional_attributes"
@@ -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
@@ -97,6 +97,12 @@ def chat_completions_create(original_method, version, tracer):
97
97
  else ""
98
98
  )
99
99
  service_provider = SERVICE_PROVIDERS["OPENAI"]
100
+ # If base url contains perplexity or azure, set the service provider accordingly
101
+ if "perplexity" in base_url:
102
+ service_provider = SERVICE_PROVIDERS["PPLX"]
103
+ elif "azure" in base_url:
104
+ service_provider = SERVICE_PROVIDERS["AZURE"]
105
+
100
106
  extra_attributes = baggage.get_baggage(LANGTRACE_ADDITIONAL_SPAN_ATTRIBUTES_KEY)
101
107
 
102
108
  span_attributes = {
@@ -214,12 +220,10 @@ def chat_completions_create(original_method, version, tracer):
214
220
  prompt_tokens,
215
221
  function_call=kwargs.get("functions") is not None,
216
222
  )
217
- except Exception as e:
218
- # Record the exception in the span
219
- span.record_exception(e)
220
- # Set the span status to indicate an error
221
- span.set_status(Status(StatusCode.ERROR, str(e)))
222
- # 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)))
223
227
  span.end()
224
228
  raise
225
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()
@@ -0,0 +1 @@
1
+ __version__ = "1.3.1"
@@ -1,7 +1,7 @@
1
1
  # from examples.langchain_example.basic import basic, load_and_split, rag
2
2
 
3
3
 
4
- from examples.openai.chat_completion import chat_completion
4
+ # from examples.openai.chat_completion import chat_completion
5
5
 
6
6
  # from examples.openai import images_generate
7
7
  # from examples.openai.function_calling import function_calling
@@ -11,12 +11,20 @@ from examples.openai.chat_completion import chat_completion
11
11
  # from examples.chroma_example.basic import basic
12
12
  # from examples.llamaindex_example.basic import basic
13
13
  # from examples.langchain_example.basic import basic
14
+ # from examples.cohere_example.chat import chat_comp
15
+ from examples.cohere_example.embed_create import embed_create
16
+ # from examples.cohere_example.chat_stream import chat_stream
17
+
18
+ # from examples.perplexity_example.basic import basic
14
19
 
15
20
  # load_and_split()
16
21
  # rag()
17
22
  # basic()
18
- chat_completion()
23
+ # chat_completion()
19
24
  # function_calling()
20
25
  # images_generate()
21
26
  # embeddings_create()
22
27
  # messages_create()
28
+ # chat_comp()
29
+ embed_create()
30
+ # chat_stream()
File without changes
@@ -1 +0,0 @@
1
- __version__ = "1.2.23"