Skip to content

Commit e67d055

Browse files
committed
kickstart cerebras
1 parent 675449c commit e67d055

File tree

5 files changed

+99
-0
lines changed

5 files changed

+99
-0
lines changed

src/examples/cerebras_example/__init__.py

Whitespace-only changes.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from langtrace_python_sdk import langtrace
2+
from cerebras.cloud.sdk import Cerebras
3+
from dotenv import load_dotenv
4+
5+
load_dotenv()
6+
7+
langtrace.init()
8+
9+
client = Cerebras()
10+
11+
12+
def completion_example():
13+
completion = client.chat.completions.create(
14+
messages=[
15+
{
16+
"role": "user",
17+
"content": "Why is fast inference important?",
18+
}
19+
],
20+
model="llama3.1-8b",
21+
)
22+
return completion

src/langtrace_python_sdk/instrumentation/cerebras/__init__.py

Whitespace-only changes.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
"""
2+
Copyright (c) 2024 Scale3 Labs
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
"""
16+
17+
from typing import Collection
18+
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
19+
from opentelemetry.trace import get_tracer
20+
from wrapt import wrap_function_wrapper
21+
from importlib_metadata import version as v
22+
from .patch import chat_completions_create, async_chat_completions_create
23+
24+
25+
class CerebrasInstrumentation(BaseInstrumentor):
26+
"""
27+
The CerebrasInstrumentation class represents the Cerebras instrumentation
28+
"""
29+
30+
def instrumentation_dependencies(self) -> Collection[str]:
31+
return ["cerebras-cloud-sdk >= 1.0.0"]
32+
33+
def _instrument(self, **kwargs):
34+
tracer_provider = kwargs.get("tracer_provider")
35+
tracer = get_tracer(__name__, "", tracer_provider)
36+
version = v("cerebras-cloud-sdk")
37+
38+
wrap_function_wrapper(
39+
module="cerebras.cloud.sdk",
40+
name="resources.chat.completions.CompletionsResource.create",
41+
wrapper=chat_completions_create(version, tracer),
42+
)
43+
44+
wrap_function_wrapper(
45+
module="cerebras.cloud.sdk",
46+
name="resources.chat.completions.AsyncCompletionsResource.create",
47+
wrapper=async_chat_completions_create(version, tracer),
48+
)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""
2+
Copyright (c) 2024 Scale3 Labs
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
"""
16+
17+
18+
def chat_completions_create(version: str, tracer):
19+
def traced_method(wrapped, instance, args, kwargs):
20+
return wrapped(*args, **kwargs)
21+
22+
return traced_method
23+
24+
25+
def async_chat_completions_create(version: str, tracer):
26+
def traced_method(wrapped, instance, args, kwargs):
27+
return wrapped(*args, **kwargs)
28+
29+
return traced_method

0 commit comments

Comments
 (0)