langtrace-python-sdk 1.3.1__tar.gz → 1.3.3__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 (90) hide show
  1. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/PKG-INFO +3 -2
  2. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/README.md +1 -1
  3. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/pyproject.toml +2 -1
  4. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/examples/anthropic_example/completion.py +1 -1
  5. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/examples/chroma_example/basic.py +1 -1
  6. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/examples/cohere_example/chat.py +1 -1
  7. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/examples/cohere_example/chat_stream.py +2 -1
  8. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/examples/cohere_example/embed_create.py +5 -4
  9. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/examples/fastapi_example/basic_route.py +4 -2
  10. langtrace_python_sdk-1.3.3/src/examples/hiveagent_example/basic.py +23 -0
  11. langtrace_python_sdk-1.3.3/src/examples/llamaindex_example/agent.py +86 -0
  12. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/examples/llamaindex_example/basic.py +1 -1
  13. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/examples/openai/chat_completion.py +1 -1
  14. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/examples/openai/function_calling.py +1 -1
  15. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/examples/perplexity_example/basic.py +5 -3
  16. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/examples/pinecone_example/basic.py +1 -1
  17. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/langtrace_python_sdk/extensions/langtrace_exporter.py +10 -2
  18. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/langtrace_python_sdk/instrumentation/cohere/patch.py +78 -51
  19. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/langtrace_python_sdk/instrumentation/llamaindex/instrumentation.py +8 -1
  20. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/langtrace_python_sdk/instrumentation/llamaindex/patch.py +46 -2
  21. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/langtrace_python_sdk/instrumentation/openai/instrumentation.py +25 -0
  22. langtrace_python_sdk-1.3.3/src/langtrace_python_sdk/instrumentation/openai/patch.py +749 -0
  23. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/langtrace_python_sdk/langtrace.py +10 -7
  24. langtrace_python_sdk-1.3.3/src/langtrace_python_sdk/version.py +1 -0
  25. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/run_example.py +5 -3
  26. langtrace_python_sdk-1.3.1/src/langtrace_python_sdk/instrumentation/openai/patch.py +0 -368
  27. langtrace_python_sdk-1.3.1/src/langtrace_python_sdk/version.py +0 -1
  28. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/.gitignore +0 -0
  29. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/LICENSE +0 -0
  30. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/__init__.py +0 -0
  31. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/examples/__init__.py +0 -0
  32. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/examples/anthropic_example/__init__.py +0 -0
  33. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/examples/chroma_example/__init__.py +0 -0
  34. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/examples/cohere_example/__init__.py +0 -0
  35. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/examples/langchain_example/__init__.py +0 -0
  36. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/examples/langchain_example/basic.py +0 -0
  37. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/examples/langchain_example/tool.py +0 -0
  38. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/examples/llamaindex_example/__init__.py +0 -0
  39. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/examples/llamaindex_example/data/abramov.txt +0 -0
  40. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/examples/openai/__init__.py +0 -0
  41. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/examples/openai/embeddings_create.py +0 -0
  42. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/examples/openai/images_generate.py +0 -0
  43. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/examples/pinecone_example/__init__.py +0 -0
  44. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/langtrace_python_sdk/__init__.py +0 -0
  45. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/langtrace_python_sdk/constants/__init__.py +0 -0
  46. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/langtrace_python_sdk/constants/exporter/langtrace_exporter.py +0 -0
  47. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/langtrace_python_sdk/constants/instrumentation/__init__.py +0 -0
  48. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/langtrace_python_sdk/constants/instrumentation/anthropic.py +0 -0
  49. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/langtrace_python_sdk/constants/instrumentation/chroma.py +0 -0
  50. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/langtrace_python_sdk/constants/instrumentation/cohere.py +0 -0
  51. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/langtrace_python_sdk/constants/instrumentation/common.py +0 -0
  52. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/langtrace_python_sdk/constants/instrumentation/openai.py +0 -0
  53. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/langtrace_python_sdk/constants/instrumentation/pinecone.py +0 -0
  54. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/langtrace_python_sdk/extensions/__init__.py +0 -0
  55. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/langtrace_python_sdk/instrumentation/__init__.py +0 -0
  56. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/langtrace_python_sdk/instrumentation/anthropic/__init__.py +0 -0
  57. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/langtrace_python_sdk/instrumentation/anthropic/instrumentation.py +0 -0
  58. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/langtrace_python_sdk/instrumentation/anthropic/patch.py +0 -0
  59. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/langtrace_python_sdk/instrumentation/chroma/__init__.py +0 -0
  60. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/langtrace_python_sdk/instrumentation/chroma/instrumentation.py +0 -0
  61. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/langtrace_python_sdk/instrumentation/chroma/patch.py +0 -0
  62. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/langtrace_python_sdk/instrumentation/cohere/__init__.py +0 -0
  63. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/langtrace_python_sdk/instrumentation/cohere/instrumentation.py +0 -0
  64. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/langtrace_python_sdk/instrumentation/langchain/__init__.py +0 -0
  65. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/langtrace_python_sdk/instrumentation/langchain/instrumentation.py +0 -0
  66. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/langtrace_python_sdk/instrumentation/langchain/patch.py +0 -0
  67. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/langtrace_python_sdk/instrumentation/langchain_community/__init__.py +0 -0
  68. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/langtrace_python_sdk/instrumentation/langchain_community/instrumentation.py +0 -0
  69. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/langtrace_python_sdk/instrumentation/langchain_community/patch.py +0 -0
  70. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/langtrace_python_sdk/instrumentation/langchain_core/__init__.py +0 -0
  71. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/langtrace_python_sdk/instrumentation/langchain_core/instrumentation.py +0 -0
  72. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/langtrace_python_sdk/instrumentation/langchain_core/patch.py +0 -0
  73. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/langtrace_python_sdk/instrumentation/llamaindex/__init__.py +0 -0
  74. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/langtrace_python_sdk/instrumentation/openai/__init__.py +0 -0
  75. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/langtrace_python_sdk/instrumentation/pinecone/__init__.py +0 -0
  76. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/langtrace_python_sdk/instrumentation/pinecone/instrumentation.py +0 -0
  77. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/langtrace_python_sdk/instrumentation/pinecone/patch.py +0 -0
  78. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/langtrace_python_sdk/utils/__init__.py +0 -0
  79. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/langtrace_python_sdk/utils/llm.py +0 -0
  80. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/langtrace_python_sdk/utils/with_root_span.py +0 -0
  81. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/tests/__init__.py +0 -0
  82. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/tests/anthropic/test_anthropic.py +0 -0
  83. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/tests/chroma/test_chroma.py +0 -0
  84. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/tests/langchain/test_langchain.py +0 -0
  85. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/tests/langchain/test_langchain_community.py +0 -0
  86. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/tests/langchain/test_langchain_core.py +0 -0
  87. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/tests/openai/test_chat_completion.py +0 -0
  88. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/tests/openai/test_image_generation.py +0 -0
  89. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/src/tests/pinecone/test_pinecone.py +0 -0
  90. {langtrace_python_sdk-1.3.1 → langtrace_python_sdk-1.3.3}/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.3.1
3
+ Version: 1.3.3
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>
@@ -19,6 +19,7 @@ Requires-Dist: trace-attributes
19
19
  Provides-Extra: dev
20
20
  Requires-Dist: anthropic; extra == 'dev'
21
21
  Requires-Dist: chromadb; extra == 'dev'
22
+ Requires-Dist: cohere; extra == 'dev'
22
23
  Requires-Dist: langchain; extra == 'dev'
23
24
  Requires-Dist: langchain-openai; extra == 'dev'
24
25
  Requires-Dist: llama-index; extra == 'dev'
@@ -144,7 +145,7 @@ pip install langtrace-python-sdk
144
145
 
145
146
  ``` python
146
147
  from langtrace_python_sdk import langtrace # Must precede any llm module imports
147
- langtrace.init(write_to_langtrace_cloud=False, batch=False)
148
+ langtrace.init(write_to_langtrace_cloud=False)
148
149
  ```
149
150
 
150
151
  ## Langtrace self hosted custom exporter
@@ -116,7 +116,7 @@ pip install langtrace-python-sdk
116
116
 
117
117
  ``` python
118
118
  from langtrace_python_sdk import langtrace # Must precede any llm module imports
119
- langtrace.init(write_to_langtrace_cloud=False, batch=False)
119
+ langtrace.init(write_to_langtrace_cloud=False)
120
120
  ```
121
121
 
122
122
  ## Langtrace self hosted custom exporter
@@ -37,7 +37,8 @@ dev = [
37
37
  "langchain-openai",
38
38
  "llama-index",
39
39
  "langchain-openai",
40
- "chromadb"
40
+ "chromadb",
41
+ "cohere"
41
42
  ]
42
43
 
43
44
 
@@ -8,7 +8,7 @@ from langtrace_python_sdk import with_langtrace_root_span
8
8
 
9
9
  _ = load_dotenv(find_dotenv())
10
10
 
11
- langtrace.init(batch=False, debug_log_to_console=True, write_to_langtrace_cloud=False)
11
+ langtrace.init(write_to_langtrace_cloud=False)
12
12
 
13
13
  @with_langtrace_root_span("messages_create")
14
14
  def messages_create():
@@ -7,7 +7,7 @@ from langtrace_python_sdk.utils.with_root_span import with_langtrace_root_span
7
7
 
8
8
  _ = load_dotenv(find_dotenv())
9
9
 
10
- langtrace.init(batch=False, debug_log_to_console=True, write_to_langtrace_cloud=False)
10
+ langtrace.init(write_to_langtrace_cloud=False)
11
11
 
12
12
 
13
13
  @with_langtrace_root_span()
@@ -6,7 +6,7 @@ from langtrace_python_sdk import langtrace
6
6
 
7
7
  _ = load_dotenv(find_dotenv())
8
8
 
9
- langtrace.init(batch=False, debug_log_to_console=True, write_to_langtrace_cloud=False)
9
+ langtrace.init(write_to_langtrace_cloud=False)
10
10
 
11
11
  co = cohere.Client()
12
12
 
@@ -2,11 +2,12 @@ from dotenv import find_dotenv, load_dotenv
2
2
  import cohere
3
3
 
4
4
  from langtrace_python_sdk import langtrace
5
+
5
6
  # from langtrace_python_sdk.utils.with_root_span import with_langtrace_root_span
6
7
 
7
8
  _ = load_dotenv(find_dotenv())
8
9
 
9
- langtrace.init(batch=False, debug_log_to_console=True, write_to_langtrace_cloud=False)
10
+ langtrace.init(write_to_langtrace_cloud=False)
10
11
 
11
12
  co = cohere.Client()
12
13
 
@@ -2,11 +2,12 @@ from dotenv import find_dotenv, load_dotenv
2
2
  import cohere
3
3
 
4
4
  from langtrace_python_sdk import langtrace
5
+
5
6
  # from langtrace_python_sdk.utils.with_root_span import with_langtrace_root_span
6
7
 
7
8
  _ = load_dotenv(find_dotenv())
8
9
 
9
- langtrace.init(batch=False, debug_log_to_console=True, write_to_langtrace_cloud=False)
10
+ langtrace.init(write_to_langtrace_cloud=False)
10
11
 
11
12
  co = cohere.Client()
12
13
 
@@ -14,8 +15,8 @@ co = cohere.Client()
14
15
  # @with_langtrace_root_span("embed_create")
15
16
  def embed_create():
16
17
  response = co.embed(
17
- texts=['hello', 'goodbye'],
18
- model='embed-english-v3.0',
19
- input_type='classification'
18
+ texts=["hello", "goodbye"],
19
+ model="embed-english-v3.0",
20
+ input_type="classification",
20
21
  )
21
22
  # print(response)
@@ -8,14 +8,16 @@ from openai import OpenAI
8
8
 
9
9
  from langtrace_python_sdk import langtrace
10
10
 
11
- langtrace.init(write_to_langtrace_cloud=False, debug_log_to_console=True)
11
+ langtrace.init(write_to_langtrace_cloud=False)
12
12
  app = FastAPI()
13
13
  client = OpenAI()
14
14
 
15
+
15
16
  @app.get("/")
16
17
  def root():
17
18
  vectorstore = FAISS.from_texts(
18
- ["Langtrace helps you ship high quality AI Apps to production."], embedding=OpenAIEmbeddings()
19
+ ["Langtrace helps you ship high quality AI Apps to production."],
20
+ embedding=OpenAIEmbeddings(),
19
21
  )
20
22
  retriever = vectorstore.as_retriever()
21
23
 
@@ -0,0 +1,23 @@
1
+ from langtrace_python_sdk import langtrace
2
+ from hive_agent import HiveAgent
3
+ from dotenv import load_dotenv
4
+ from openai import OpenAI
5
+
6
+ load_dotenv()
7
+
8
+ langtrace.init(
9
+ write_to_langtrace_cloud=False,
10
+ api_host="http://localhost:3000",
11
+ )
12
+
13
+ client = OpenAI()
14
+
15
+
16
+ def basic():
17
+ my_agent = HiveAgent(
18
+ name="my_agent",
19
+ functions=[],
20
+ instruction="your instructions for this agent's goal",
21
+ )
22
+
23
+ my_agent.run_server()
@@ -0,0 +1,86 @@
1
+ import json
2
+ from typing import Sequence, List
3
+
4
+ from llama_index.agent.openai import OpenAIAgent
5
+ from llama_index.llms.openai import OpenAI
6
+ from llama_index.core.llms import ChatMessage
7
+ from llama_index.core.tools import BaseTool, FunctionTool
8
+ from openai.types.chat import ChatCompletionMessageToolCall
9
+ from langtrace_python_sdk import langtrace
10
+
11
+ import nest_asyncio
12
+
13
+ nest_asyncio.apply()
14
+
15
+ langtrace.init(write_to_langtrace_cloud=False)
16
+
17
+
18
+ def multiply(a: int, b: int) -> int:
19
+ """Multiple two integers and returns the result integer"""
20
+ return a * b
21
+
22
+
23
+ multiply_tool = FunctionTool.from_defaults(fn=multiply)
24
+
25
+
26
+ def add(a: int, b: int) -> int:
27
+ """Add two integers and returns the result integer"""
28
+ return a + b
29
+
30
+
31
+ add_tool = FunctionTool.from_defaults(fn=add)
32
+
33
+
34
+ class YourOpenAIAgent:
35
+ def __init__(
36
+ self,
37
+ tools: Sequence[BaseTool] = [],
38
+ llm: OpenAI = OpenAI(temperature=0, model="gpt-3.5-turbo-0613"),
39
+ chat_history: List[ChatMessage] = [],
40
+ ) -> None:
41
+ self._llm = llm
42
+ self._tools = {tool.metadata.name: tool for tool in tools}
43
+ self._chat_history = chat_history
44
+
45
+ def reset(self) -> None:
46
+ self._chat_history = []
47
+
48
+ def chat(self, message: str) -> str:
49
+ chat_history = self._chat_history
50
+ chat_history.append(ChatMessage(role="user", content=message))
51
+ tools = [tool.metadata.to_openai_tool() for _, tool in self._tools.items()]
52
+
53
+ ai_message = self._llm.chat(chat_history, tools=tools).message
54
+ additional_kwargs = ai_message.additional_kwargs
55
+ chat_history.append(ai_message)
56
+
57
+ tool_calls = additional_kwargs.get("tool_calls", None)
58
+ # parallel function calling is now supported
59
+ if tool_calls is not None:
60
+ for tool_call in tool_calls:
61
+ function_message = self._call_function(tool_call)
62
+ chat_history.append(function_message)
63
+ ai_message = self._llm.chat(chat_history).message
64
+ chat_history.append(ai_message)
65
+
66
+ return ai_message.content
67
+
68
+ def _call_function(self, tool_call: ChatCompletionMessageToolCall) -> ChatMessage:
69
+ id_ = tool_call.id
70
+ function_call = tool_call.function
71
+ tool = self._tools[function_call.name]
72
+ output = tool(**json.loads(function_call.arguments))
73
+ return ChatMessage(
74
+ name=function_call.name,
75
+ content=str(output),
76
+ role="tool",
77
+ additional_kwargs={
78
+ "tool_call_id": id_,
79
+ "name": function_call.name,
80
+ },
81
+ )
82
+
83
+
84
+ # agent = YourOpenAIAgent(tools=[multiply_tool, add_tool])
85
+ llm = OpenAI(model="gpt-3.5-turbo-0613")
86
+ agent = OpenAIAgent.from_tools([multiply_tool, add_tool], llm=llm, verbose=True)
@@ -7,7 +7,7 @@ from langtrace_python_sdk.utils.with_root_span import with_langtrace_root_span
7
7
  _ = load_dotenv(find_dotenv())
8
8
 
9
9
 
10
- langtrace.init(batch=False, debug_log_to_console=True, write_to_langtrace_cloud=False)
10
+ langtrace.init(write_to_langtrace_cloud=False)
11
11
 
12
12
 
13
13
  @with_langtrace_root_span()
@@ -7,7 +7,7 @@ from langtrace_python_sdk.utils.with_root_span import (
7
7
 
8
8
  _ = load_dotenv(find_dotenv())
9
9
 
10
- langtrace.init()
10
+ langtrace.init(write_to_langtrace_cloud=True)
11
11
  client = OpenAI()
12
12
 
13
13
 
@@ -8,7 +8,7 @@ from langtrace_python_sdk.utils.with_root_span import with_langtrace_root_span
8
8
 
9
9
  _ = load_dotenv(find_dotenv())
10
10
 
11
- langtrace.init(batch=False, debug_log_to_console=True, write_to_langtrace_cloud=False)
11
+ langtrace.init(write_to_langtrace_cloud=False)
12
12
 
13
13
  client = OpenAI()
14
14
 
@@ -2,11 +2,13 @@ from openai import OpenAI
2
2
 
3
3
  from langtrace_python_sdk import langtrace
4
4
  from langtrace_python_sdk.utils.with_root_span import (
5
- with_additional_attributes, with_langtrace_root_span)
5
+ with_additional_attributes,
6
+ with_langtrace_root_span,
7
+ )
6
8
 
7
9
  # _ = load_dotenv(find_dotenv())
8
10
 
9
- langtrace.init(batch=False, write_to_langtrace_cloud=False, debug_log_to_console=True)
11
+ langtrace.init(write_to_langtrace_cloud=False)
10
12
  client = OpenAI(base_url="https://api.perplexity.ai", api_key="PPLX_API_KEY")
11
13
 
12
14
 
@@ -18,4 +20,4 @@ def basic():
18
20
  stream=False,
19
21
  )
20
22
  print(response)
21
- return response
23
+ return response
@@ -11,7 +11,7 @@ from langtrace_python_sdk.utils.with_root_span import with_langtrace_root_span
11
11
 
12
12
  _ = load_dotenv(find_dotenv())
13
13
 
14
- langtrace.init(batch=False, debug_log_to_console=True, write_to_langtrace_cloud=False)
14
+ langtrace.init(write_to_langtrace_cloud=False)
15
15
 
16
16
  client = OpenAI()
17
17
  pinecone = Pinecone()
@@ -6,7 +6,9 @@ import requests
6
6
  from opentelemetry.sdk.trace.export import ReadableSpan, SpanExporter, SpanExportResult
7
7
  from opentelemetry.trace.span import format_trace_id
8
8
 
9
- from langtrace_python_sdk.constants.exporter.langtrace_exporter import LANGTRACE_REMOTE_URL
9
+ from langtrace_python_sdk.constants.exporter.langtrace_exporter import (
10
+ LANGTRACE_REMOTE_URL,
11
+ )
10
12
 
11
13
 
12
14
  class LangTraceExporter(SpanExporter):
@@ -48,7 +50,12 @@ class LangTraceExporter(SpanExporter):
48
50
  api_key: str
49
51
  write_to_remote_url: bool
50
52
 
51
- def __init__(self, api_key: str = None, write_to_remote_url: bool = False, api_host: typing.Optional[str] = None) -> None:
53
+ def __init__(
54
+ self,
55
+ api_key: str = None,
56
+ write_to_remote_url: bool = False,
57
+ api_host: typing.Optional[str] = None,
58
+ ) -> None:
52
59
  self.api_key = api_key or os.environ.get("LANGTRACE_API_KEY")
53
60
  self.write_to_remote_url = write_to_remote_url
54
61
  self.api_host: str = api_host or LANGTRACE_REMOTE_URL
@@ -89,6 +96,7 @@ class LangTraceExporter(SpanExporter):
89
96
  data=json.dumps(data),
90
97
  headers={"Content-Type": "application/json", "x-api-key": self.api_key},
91
98
  )
99
+ print(f"sent to {self.api_host}/api/trace with {len(data)} spans")
92
100
  return SpanExportResult.SUCCESS
93
101
  except Exception as e:
94
102
  return SpanExportResult.FAILURE
@@ -9,7 +9,10 @@ from opentelemetry.trace import SpanKind
9
9
  from opentelemetry.trace.status import Status, StatusCode
10
10
 
11
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)
12
+ from langtrace_python_sdk.constants.instrumentation.common import (
13
+ LANGTRACE_ADDITIONAL_SPAN_ATTRIBUTES_KEY,
14
+ SERVICE_PROVIDERS,
15
+ )
13
16
  from langtrace_python_sdk.utils.llm import estimate_tokens
14
17
 
15
18
 
@@ -33,7 +36,7 @@ def embed_create(original_method, version, tracer):
33
36
  "llm.embedding_dataset_id": kwargs.get("dataset_id"),
34
37
  "llm.embedding_input_type": kwargs.get("input_type"),
35
38
  "llm.embedding_job_name": kwargs.get("name"),
36
- **(extra_attributes if extra_attributes is not None else {})
39
+ **(extra_attributes if extra_attributes is not None else {}),
37
40
  }
38
41
 
39
42
  attributes = LLMSpanAttributes(**span_attributes)
@@ -41,9 +44,7 @@ def embed_create(original_method, version, tracer):
41
44
  if kwargs.get("user") is not None:
42
45
  attributes.llm_user = kwargs.get("user")
43
46
 
44
- span = tracer.start_span(
45
- APIS["EMBED_CREATE"]["METHOD"], kind=SpanKind.CLIENT
46
- )
47
+ span = tracer.start_span(APIS["EMBED_CREATE"]["METHOD"], kind=SpanKind.CLIENT)
47
48
  for field, value in attributes.model_dump(by_alias=True).items():
48
49
  if value is not None:
49
50
  span.set_attribute(field, value)
@@ -59,6 +60,7 @@ def embed_create(original_method, version, tracer):
59
60
  span.set_status(Status(StatusCode.ERROR, str(error)))
60
61
  span.end()
61
62
  raise
63
+
62
64
  return traced_method
63
65
 
64
66
 
@@ -69,16 +71,12 @@ def chat_create(original_method, version, tracer):
69
71
  service_provider = SERVICE_PROVIDERS["COHERE"]
70
72
 
71
73
  message = kwargs.get("message", "")
72
- prompts = json.dumps([
73
- {
74
- "role": "USER",
75
- "content": message
76
- }
77
- ])
74
+ prompts = json.dumps([{"role": "USER", "content": message}])
78
75
  preamble = kwargs.get("preamble")
79
76
  if preamble:
80
77
  prompts = json.dumps(
81
- [{"role": "system", "content": preamble}] + [{"role": "USER", "content": message}]
78
+ [{"role": "system", "content": preamble}]
79
+ + [{"role": "USER", "content": message}]
82
80
  )
83
81
 
84
82
  chat_history = kwargs.get("chat_history")
@@ -90,8 +88,10 @@ def chat_create(original_method, version, tracer):
90
88
  item.get("role") if item.get("role") is not None else "USER"
91
89
  ),
92
90
  "content": (
93
- item.get("message") if item.get("message") is not None else ""
94
- )
91
+ item.get("message")
92
+ if item.get("message") is not None
93
+ else ""
94
+ ),
95
95
  }
96
96
  }
97
97
  for item in chat_history
@@ -108,10 +108,12 @@ def chat_create(original_method, version, tracer):
108
108
  "langtrace.version": "1.0.0",
109
109
  "url.full": APIS["CHAT_CREATE"]["URL"],
110
110
  "llm.api": APIS["CHAT_CREATE"]["ENDPOINT"],
111
- "llm.model": kwargs.get("model") if kwargs.get("model") is not None else "command-r",
111
+ "llm.model": (
112
+ kwargs.get("model") if kwargs.get("model") is not None else "command-r"
113
+ ),
112
114
  "llm.stream": False,
113
115
  "llm.prompts": prompts,
114
- **(extra_attributes if extra_attributes is not None else {})
116
+ **(extra_attributes if extra_attributes is not None else {}),
115
117
  }
116
118
 
117
119
  attributes = LLMSpanAttributes(**span_attributes)
@@ -146,9 +148,7 @@ def chat_create(original_method, version, tracer):
146
148
  # stringify the list of objects
147
149
  attributes.llm_tool_results = json.dumps(kwargs.get("tool_results"))
148
150
 
149
- span = tracer.start_span(
150
- APIS["CHAT_CREATE"]["METHOD"], kind=SpanKind.CLIENT
151
- )
151
+ span = tracer.start_span(APIS["CHAT_CREATE"]["METHOD"], kind=SpanKind.CLIENT)
152
152
 
153
153
  # Set the attributes on the span
154
154
  for field, value in attributes.model_dump(by_alias=True).items():
@@ -159,37 +159,47 @@ def chat_create(original_method, version, tracer):
159
159
  result = wrapped(*args, **kwargs)
160
160
 
161
161
  # Set the response attributes
162
- if (hasattr(result, "generation_id")) and (result.generation_id is not None):
162
+ if (hasattr(result, "generation_id")) and (
163
+ result.generation_id is not None
164
+ ):
163
165
  span.set_attribute("llm.generation_id", result.generation_id)
164
166
  if (hasattr(result, "response_id")) and (result.response_id is not None):
165
167
  span.set_attribute("llm.response_id", result.response_id)
166
- if (hasattr(result, "is_search_required")) and (result.is_search_required is not None):
168
+ if (hasattr(result, "is_search_required")) and (
169
+ result.is_search_required is not None
170
+ ):
167
171
  span.set_attribute("llm.is_search_required", result.is_search_required)
168
172
 
169
173
  if kwargs.get("stream") is False or kwargs.get("stream") is None:
170
174
  if hasattr(result, "text") and result.text is not None:
171
- if hasattr(result, "chat_history") and result.chat_history is not None:
175
+ if (
176
+ hasattr(result, "chat_history")
177
+ and result.chat_history is not None
178
+ ):
172
179
  responses = [
173
180
  {
174
181
  "message": {
175
182
  "role": (
176
- item.role if hasattr(item, "role") and item.role is not None else "USER"
183
+ item.role
184
+ if hasattr(item, "role")
185
+ and item.role is not None
186
+ else "USER"
177
187
  ),
178
188
  "content": (
179
- item.message if hasattr(item, "message") and item.message is not None else ""
180
- )
189
+ item.message
190
+ if hasattr(item, "message")
191
+ and item.message is not None
192
+ else ""
193
+ ),
181
194
  }
182
195
  }
183
196
  for item in result.chat_history
184
197
  ]
185
198
  span.set_attribute("llm.responses", json.dumps(responses))
186
199
  else:
187
- responses = [{
188
- "message": {
189
- "role": "CHATBOT",
190
- "content": result.text
191
- }
192
- }]
200
+ responses = [
201
+ {"message": {"role": "CHATBOT", "content": result.text}}
202
+ ]
193
203
  span.set_attribute("llm.responses", json.dumps(responses))
194
204
  else:
195
205
  responses = []
@@ -197,15 +207,33 @@ def chat_create(original_method, version, tracer):
197
207
 
198
208
  # Get the usage
199
209
  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:
210
+ if (
211
+ hasattr(result.meta, "billed_units")
212
+ and result.meta.billed_units is not None
213
+ ):
201
214
  usage = result.meta.billed_units
202
215
  if usage is not None:
203
216
  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,
217
+ "input_tokens": (
218
+ usage.input_tokens
219
+ if usage.input_tokens is not None
220
+ else 0
221
+ ),
222
+ "output_tokens": (
223
+ usage.output_tokens
224
+ if usage.output_tokens is not None
225
+ else 0
226
+ ),
227
+ "total_tokens": (
228
+ usage.input_tokens + usage.output_tokens
229
+ if usage.input_tokens is not None
230
+ and usage.output_tokens is not None
231
+ else 0
232
+ ),
207
233
  }
208
- span.set_attribute("llm.token.counts", json.dumps(usage_dict))
234
+ span.set_attribute(
235
+ "llm.token.counts", json.dumps(usage_dict)
236
+ )
209
237
  span.set_status(StatusCode.OK)
210
238
  span.end()
211
239
  return result
@@ -218,6 +246,7 @@ def chat_create(original_method, version, tracer):
218
246
  span.set_status(Status(StatusCode.ERROR, str(error)))
219
247
  span.end()
220
248
  raise
249
+
221
250
  return traced_method
222
251
 
223
252
 
@@ -229,16 +258,12 @@ def chat_stream(original_method, version, tracer):
229
258
 
230
259
  message = kwargs.get("message", "")
231
260
  prompt_tokens = estimate_tokens(message)
232
- prompts = json.dumps([
233
- {
234
- "role": "USER",
235
- "content": message
236
- }
237
- ])
261
+ prompts = json.dumps([{"role": "USER", "content": message}])
238
262
  preamble = kwargs.get("preamble")
239
263
  if preamble:
240
264
  prompts = json.dumps(
241
- [{"role": "system", "content": preamble}] + [{"role": "USER", "content": message}]
265
+ [{"role": "system", "content": preamble}]
266
+ + [{"role": "USER", "content": message}]
242
267
  )
243
268
 
244
269
  chat_history = kwargs.get("chat_history")
@@ -250,8 +275,10 @@ def chat_stream(original_method, version, tracer):
250
275
  item.get("role") if item.get("role") is not None else "USER"
251
276
  ),
252
277
  "content": (
253
- item.get("message") if item.get("message") is not None else ""
254
- )
278
+ item.get("message")
279
+ if item.get("message") is not None
280
+ else ""
281
+ ),
255
282
  }
256
283
  }
257
284
  for item in chat_history
@@ -268,10 +295,12 @@ def chat_stream(original_method, version, tracer):
268
295
  "langtrace.version": "1.0.0",
269
296
  "url.full": APIS["CHAT_STREAM"]["URL"],
270
297
  "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,
298
+ "llm.model": (
299
+ kwargs.get("model") if kwargs.get("model") is not None else "command-r"
300
+ ),
301
+ "llm.stream": True,
273
302
  "llm.prompts": prompts,
274
- **(extra_attributes if extra_attributes is not None else {})
303
+ **(extra_attributes if extra_attributes is not None else {}),
275
304
  }
276
305
 
277
306
  attributes = LLMSpanAttributes(**span_attributes)
@@ -306,9 +335,7 @@ def chat_stream(original_method, version, tracer):
306
335
  # stringify the list of objects
307
336
  attributes.llm_tool_results = json.dumps(kwargs.get("tool_results"))
308
337
 
309
- span = tracer.start_span(
310
- APIS["CHAT_CREATE"]["METHOD"], kind=SpanKind.CLIENT
311
- )
338
+ span = tracer.start_span(APIS["CHAT_CREATE"]["METHOD"], kind=SpanKind.CLIENT)
312
339
  for field, value in attributes.model_dump(by_alias=True).items():
313
340
  if value is not None:
314
341
  span.set_attribute(field, value)
@@ -10,7 +10,14 @@ from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
10
10
  from opentelemetry.trace import get_tracer
11
11
  from wrapt import wrap_function_wrapper
12
12
 
13
- from langtrace_python_sdk.instrumentation.llamaindex.patch import generic_patch
13
+ from langtrace_python_sdk.instrumentation.llamaindex.patch import (
14
+ generic_patch,
15
+ async_generic_patch,
16
+ )
17
+
18
+ import logging
19
+
20
+ logging.basicConfig(level=logging.FATAL)
14
21
 
15
22
  import logging
16
23