Skip to content

Commit f74ab29

Browse files
authored
Merge pull request #227 from Scale3-Labs/ali/s3en-2466-migrate-to-otel-genai-spec
Migrate Langtrace Python SDK to OTEL GenAI naming standards
2 parents d38556f + 91491a3 commit f74ab29

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+7219
-1275
lines changed

pyproject.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ classifiers=[
1818
"Operating System :: OS Independent",
1919
]
2020
dependencies = [
21-
'trace-attributes>=5.0.0,<6.0.0',
21+
'trace-attributes>=6.0.0,<7.0.0',
2222
'opentelemetry-api>=1.25.0',
2323
'opentelemetry-sdk>=1.25.0',
2424
'opentelemetry-instrumentation>=0.46b0',
@@ -49,7 +49,10 @@ dev = [
4949
"cohere",
5050
"qdrant_client",
5151
"weaviate-client",
52-
"ollama"
52+
"ollama",
53+
"groq",
54+
"google-generativeai",
55+
"google-cloud-aiplatform"
5356
]
5457

5558
test = [

src/examples/cohere_example/chat.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ def chat_comp():
2323
"message": "The man who is widely credited with discovering gravity is Sir Isaac Newton",
2424
},
2525
],
26+
k=3,
2627
message="Tell me a story in 3 sentences or less?",
2728
preamble="answer like a pirate",
2829
# perform web search before answering the question. You can also use your own custom connector.

src/examples/cohere_example/chat_stream.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ def chat_stream():
1818
message="Tell me a short story in 2 lines",
1919
preamble="Respond like a pirate",
2020
max_tokens=100,
21+
k=3,
22+
p=0.9,
23+
temperature=0.5,
2124
):
2225
if event.event_type == "text-generation":
2326
result.append(event.text)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from .main import basic
2+
3+
4+
class GeminiRunner:
5+
def run(self):
6+
basic()
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
tools = [
2+
{
3+
"function_declarations": [
4+
{
5+
"name": "find_movies",
6+
"description": "find movie titles currently playing in theaters based on any description, genre, title words, etc.",
7+
"parameters": {
8+
"type": "object",
9+
"properties": {
10+
"location": {
11+
"type": "string",
12+
"description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616",
13+
},
14+
"description": {
15+
"type": "string",
16+
"description": "Any kind of description including category or genre, title words, attributes, etc.",
17+
},
18+
},
19+
"required": ["description"],
20+
},
21+
},
22+
{
23+
"name": "find_theaters",
24+
"description": "find theaters based on location and optionally movie title which is currently playing in theaters",
25+
"parameters": {
26+
"type": "object",
27+
"properties": {
28+
"location": {
29+
"type": "string",
30+
"description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616",
31+
},
32+
"movie": {"type": "string", "description": "Any movie title"},
33+
},
34+
"required": ["location"],
35+
},
36+
},
37+
{
38+
"name": "get_showtimes",
39+
"description": "Find the start times for movies playing in a specific theater",
40+
"parameters": {
41+
"type": "object",
42+
"properties": {
43+
"location": {
44+
"type": "string",
45+
"description": "The city and state, e.g. San Francisco, CA or a zip code e.g. 95616",
46+
},
47+
"movie": {"type": "string", "description": "Any movie title"},
48+
"theater": {
49+
"type": "string",
50+
"description": "Name of the theater",
51+
},
52+
"date": {
53+
"type": "string",
54+
"description": "Date for requested showtime",
55+
},
56+
},
57+
"required": ["location", "movie", "theater", "date"],
58+
},
59+
},
60+
]
61+
}
62+
]
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
from langtrace_python_sdk import langtrace
2+
import google.generativeai as genai
3+
from dotenv import load_dotenv
4+
import os
5+
import asyncio
6+
import pathlib
7+
from .function_tools import tools
8+
9+
load_dotenv()
10+
11+
langtrace.init(write_spans_to_console=False, batch=False)
12+
genai.configure(api_key=os.environ["GEMINI_API_KEY"])
13+
14+
15+
async def async_demo():
16+
task1 = asyncio.create_task(async_generate())
17+
task2 = asyncio.create_task(async_generate(stream=True))
18+
return await asyncio.gather(task1, task2)
19+
20+
21+
def basic():
22+
generate()
23+
generate(stream=True, with_tools=True)
24+
25+
# image_to_text()
26+
# audio_to_text()
27+
asyncio.run(async_demo())
28+
29+
30+
def generate(stream=False, with_tools=False):
31+
model = genai.GenerativeModel(
32+
"gemini-1.5-pro", system_instruction="You are a cat. Your name is Neko."
33+
)
34+
35+
response = model.generate_content(
36+
"Write a story about a AI and magic",
37+
stream=stream,
38+
tools=tools if with_tools else None,
39+
)
40+
if stream:
41+
for res in response:
42+
if res.text:
43+
print(res.text)
44+
else:
45+
print(response.text)
46+
47+
48+
async def async_generate(stream=False):
49+
model = genai.GenerativeModel(
50+
"gemini-1.5-pro", system_instruction="You are a cat. Your name is Neko."
51+
)
52+
response = await model.generate_content_async(
53+
"Write a story about a AI and magic", stream=stream
54+
)
55+
if stream:
56+
async for chunk in response:
57+
if chunk.text:
58+
print(chunk.text)
59+
else:
60+
print(response.text)
61+
62+
63+
def image_to_text(stream=False):
64+
model = genai.GenerativeModel("gemini-1.5-flash")
65+
image1 = {
66+
"mime_type": "image/jpeg",
67+
"data": pathlib.Path("src/examples/gemini_example/jetpack.jpg").read_bytes(),
68+
}
69+
70+
prompt = "Describe me this picture. What do you see in it."
71+
response = model.generate_content([prompt, image1], stream=stream)
72+
if stream:
73+
for res in response:
74+
print(res.text)
75+
else:
76+
print(response.text)
77+
78+
79+
# def audio_to_text(stream=False):
80+
# model = genai.GenerativeModel("gemini-1.5-flash")
81+
# audio = genai.upload_file(
82+
# pathlib.Path("src/examples/gemini_example/voice_note.mp3")
83+
# )
84+
85+
# prompt = "Summarize this voice recording."
86+
# response = model.generate_content([prompt, audio], stream=stream)
87+
# if stream:
88+
# for res in response:
89+
# print(res.text)
90+
# else:
91+
# print(response.text)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
from .basic import basic_app, rag, load_and_split
22
from langtrace_python_sdk import with_langtrace_root_span
33

4+
from .groq_example import groq_basic, groq_streaming
5+
46

57
class LangChainRunner:
68
@with_langtrace_root_span("LangChain")
79
def run(self):
810
basic_app()
911
rag()
1012
load_and_split()
13+
14+
15+
class GroqRunner:
16+
@with_langtrace_root_span("Groq")
17+
def run(self):
18+
groq_streaming()
Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from dotenv import find_dotenv, load_dotenv
22
from langchain_core.prompts import ChatPromptTemplate
33
from langchain_groq import ChatGroq
4+
from groq import Groq
45

56
_ = load_dotenv(find_dotenv())
67

@@ -12,21 +13,33 @@
1213

1314
langtrace.init()
1415

16+
client = Groq()
1517

16-
def groq_example():
1718

18-
chat = ChatGroq(temperature=0, model_name="mixtral-8x7b-32768")
19-
20-
system = "You are a helpful assistant."
21-
human = "{text}"
22-
prompt = ChatPromptTemplate.from_messages([("system", system), ("human", human)])
23-
24-
chain = prompt | chat
25-
result = chain.invoke(
26-
{"text": "Explain the importance of low latency LLMs in 2 sentences or less."}
19+
def groq_basic():
20+
chat_completion = client.chat.completions.create(
21+
messages=[
22+
{
23+
"role": "user",
24+
"content": "Explain the importance of low latency LLMs",
25+
}
26+
],
27+
stream=False,
28+
model="llama3-8b-8192",
2729
)
28-
# print(result)
29-
return result
30-
31-
32-
groq_example()
30+
return chat_completion
31+
32+
33+
def groq_streaming():
34+
chat_completion = client.chat.completions.create(
35+
messages=[
36+
{
37+
"role": "user",
38+
"content": "Explain the importance of low latency LLMs",
39+
}
40+
],
41+
stream=True,
42+
model="llama3-8b-8192",
43+
)
44+
for chunk in chat_completion:
45+
print(chunk)

src/examples/ollama_example/basic.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ def chat():
1717
"content": "hi",
1818
},
1919
],
20+
options={"temperature": 0.5},
2021
stream=True,
2122
)
2223

src/examples/openai_example/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ def run(self):
1010
run_conversation as run_conversation_streaming,
1111
)
1212
from .chat_completion import chat_completion as chat_completion_example
13+
1314
from .embeddings_create import embeddings_create as embeddings_create_example
1415
from .function_calling import function_calling as function_example
1516
from .images_edit import image_edit

0 commit comments

Comments
 (0)