Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 14 additions & 18 deletions python/tracing/together/01_chat_completion.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from __future__ import annotations

from respan import workflow

from _shared import (
example_attributes,
first_message_text,
Expand All @@ -13,36 +11,34 @@
print_start,
workflow_name,
)
from respan import workflow

EXAMPLE_NAME = "chat-completion"


@workflow(name=workflow_name(EXAMPLE_NAME))
def _chat_completion_workflow(client) -> str:
response = client.chat.completions.create(
model=model_name(),
messages=[
{
"role": "user",
"content": "Reply with one concise sentence about tracing Together AI apps.",
}
],
max_tokens=80,
temperature=0,
)
return first_message_text(response)
def _chat_completion_workflow(prompt: str) -> str:
with make_client() as client:
response = client.chat.completions.create(
model=model_name(),
messages=[{"role": "user", "content": prompt}],
max_tokens=80,
temperature=0,
)
return first_message_text(response)


def run_chat_completion() -> None:
respan = make_respan(EXAMPLE_NAME)
client = make_client()
custom_identifier = make_custom_identifier(EXAMPLE_NAME)
respan = make_respan(EXAMPLE_NAME, custom_identifier)
text = ""

try:
with example_attributes(EXAMPLE_NAME, custom_identifier):
print_start(EXAMPLE_NAME, custom_identifier)
text = _chat_completion_workflow(client)
text = _chat_completion_workflow(
"Reply with one concise sentence about tracing Together AI apps."
)
finally:
respan.shutdown()

Expand Down
52 changes: 24 additions & 28 deletions python/tracing/together/02_stream_chat.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from __future__ import annotations

from respan import workflow

from _shared import (
example_attributes,
make_client,
Expand All @@ -12,46 +10,44 @@
print_start,
workflow_name,
)
from respan import workflow

EXAMPLE_NAME = "stream-chat"


@workflow(name=workflow_name(EXAMPLE_NAME))
def _stream_chat_workflow(client) -> str:
stream = client.chat.completions.create(
model=model_name(),
messages=[
{
"role": "user",
"content": "Stream a seven-word sentence about observability.",
}
],
max_tokens=80,
temperature=0,
stream=True,
)
parts: list[str] = []
for chunk in stream:
choices = getattr(chunk, "choices", None) or []
if not choices:
continue
delta = getattr(choices[0], "delta", None)
content = getattr(delta, "content", None)
if content:
parts.append(content)
return "".join(parts)
def _stream_chat_workflow(prompt: str) -> str:
with make_client() as client:
stream = client.chat.completions.create(
model=model_name(),
messages=[{"role": "user", "content": prompt}],
max_tokens=80,
temperature=0,
stream=True,
)
parts: list[str] = []
for chunk in stream:
choices = getattr(chunk, "choices", None) or []
if not choices:
continue
delta = getattr(choices[0], "delta", None)
content = getattr(delta, "content", None)
if content:
parts.append(content)
return "".join(parts)


def run_stream_chat() -> None:
respan = make_respan(EXAMPLE_NAME)
client = make_client()
custom_identifier = make_custom_identifier(EXAMPLE_NAME)
respan = make_respan(EXAMPLE_NAME, custom_identifier)
text = ""

try:
with example_attributes(EXAMPLE_NAME, custom_identifier):
print_start(EXAMPLE_NAME, custom_identifier)
text = _stream_chat_workflow(client)
text = _stream_chat_workflow(
"Stream a short sentence about observable trace data."
)
finally:
respan.shutdown()

Expand Down
33 changes: 14 additions & 19 deletions python/tracing/together/03_async_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import asyncio

from respan import workflow

from _shared import (
example_attributes,
first_message_text,
Expand All @@ -15,38 +13,35 @@
print_start,
workflow_name,
)
from respan import workflow

EXAMPLE_NAME = "async-chat"


@workflow(name=workflow_name(EXAMPLE_NAME))
async def _async_chat_workflow(client) -> str:
response = await client.chat.completions.create(
model=model_name(),
messages=[
{
"role": "user",
"content": "Reply with one concise sentence about async tracing.",
}
],
max_tokens=80,
temperature=0,
)
return first_message_text(response)
async def _async_chat_workflow(prompt: str) -> str:
async with make_async_client() as async_client:
response = await async_client.chat.completions.create(
model=model_name(),
messages=[{"role": "user", "content": prompt}],
max_tokens=80,
temperature=0,
)
return first_message_text(response)


async def run_async_chat() -> None:
respan = make_respan(EXAMPLE_NAME)
client = make_async_client()
custom_identifier = make_custom_identifier(EXAMPLE_NAME)
respan = make_respan(EXAMPLE_NAME, custom_identifier)
text = ""

try:
with example_attributes(EXAMPLE_NAME, custom_identifier):
print_start(EXAMPLE_NAME, custom_identifier)
text = await _async_chat_workflow(client)
text = await _async_chat_workflow(
"Reply with one concise sentence about async tracing."
)
finally:
await client.close()
respan.shutdown()

print_result(EXAMPLE_NAME, custom_identifier, text)
Expand Down
20 changes: 8 additions & 12 deletions python/tracing/together/04_text_completion.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from __future__ import annotations

from respan import workflow

from _shared import (
completion_model_name,
example_attributes,
Expand All @@ -11,38 +9,36 @@
make_respan,
print_result,
print_start,
SDK_UNAVAILABLE_ERRORS,
unavailable_text,
workflow_name,
)
from respan import workflow

EXAMPLE_NAME = "text-completion"


@workflow(name=workflow_name(EXAMPLE_NAME))
def _text_completion_workflow(client) -> str:
try:
def _text_completion_workflow(prompt: str) -> str:
with make_client() as client:
response = client.completions.create(
model=completion_model_name(),
prompt="Complete this sentence in under ten words: Tracing AI calls helps",
prompt=prompt,
max_tokens=40,
temperature=0,
)
return first_text_completion(response)
except SDK_UNAVAILABLE_ERRORS as exc:
return unavailable_text("text completions", exc)


def run_text_completion() -> None:
respan = make_respan(EXAMPLE_NAME)
client = make_client()
custom_identifier = make_custom_identifier(EXAMPLE_NAME)
respan = make_respan(EXAMPLE_NAME, custom_identifier)
text = ""

try:
with example_attributes(EXAMPLE_NAME, custom_identifier):
print_start(EXAMPLE_NAME, custom_identifier)
text = _text_completion_workflow(client)
text = _text_completion_workflow(
"Complete this sentence in under ten words: Tracing AI calls helps"
)
finally:
respan.shutdown()

Expand Down
30 changes: 14 additions & 16 deletions python/tracing/together/05_embeddings.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from __future__ import annotations

from respan import workflow

from _shared import (
embedding_model_name,
example_attributes,
Expand All @@ -10,41 +8,41 @@
make_respan,
print_result,
print_start,
SDK_UNAVAILABLE_ERRORS,
unavailable_text,
workflow_name,
)
from respan import workflow

EXAMPLE_NAME = "embeddings"


@workflow(name=workflow_name(EXAMPLE_NAME))
def _embeddings_workflow(client) -> str:
try:
def _embeddings_workflow(texts: list[str]) -> str:
with make_client() as client:
response = client.embeddings.create(
model=embedding_model_name(),
input=[
"Respan traces Together AI chat calls.",
"Embeddings should not export vector payloads.",
],
input=texts,
)
data = getattr(response, "data", None) or []
first_embedding = getattr(data[0], "embedding", []) if data else []
return f"embedding_count={len(data)} embedding_dimensions={len(first_embedding)}"
except SDK_UNAVAILABLE_ERRORS as exc:
return unavailable_text("embeddings", exc)
return (
f"embedding_count={len(data)} embedding_dimensions={len(first_embedding)}"
)


def run_embeddings() -> None:
respan = make_respan(EXAMPLE_NAME)
client = make_client()
custom_identifier = make_custom_identifier(EXAMPLE_NAME)
respan = make_respan(EXAMPLE_NAME, custom_identifier)
text = ""

try:
with example_attributes(EXAMPLE_NAME, custom_identifier):
print_start(EXAMPLE_NAME, custom_identifier)
text = _embeddings_workflow(client)
text = _embeddings_workflow(
[
"Respan traces Together AI chat calls.",
"Embeddings retain complete vector data.",
]
)
finally:
respan.shutdown()

Expand Down
18 changes: 6 additions & 12 deletions python/tracing/together/06_rerank.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from __future__ import annotations

from respan import workflow

from _shared import (
example_attributes,
make_client,
Expand All @@ -10,20 +8,19 @@
print_result,
print_start,
rerank_model_name,
SDK_UNAVAILABLE_ERRORS,
unavailable_text,
workflow_name,
)
from respan import workflow

EXAMPLE_NAME = "rerank"


@workflow(name=workflow_name(EXAMPLE_NAME))
def _rerank_workflow(client) -> str:
try:
def _rerank_workflow(query: str) -> str:
with make_client() as client:
response = client.rerank.create(
model=rerank_model_name(),
query="Which document is about observability?",
query=query,
documents=[
"Distributed tracing shows how requests move through services.",
"Sourdough bread needs flour, water, salt, and patience.",
Expand All @@ -37,20 +34,17 @@ def _rerank_workflow(client) -> str:
return "no rerank results"
top = results[0]
return f"top_index={top.index} relevance_score={top.relevance_score}"
except SDK_UNAVAILABLE_ERRORS as exc:
return unavailable_text("rerank", exc)


def run_rerank() -> None:
respan = make_respan(EXAMPLE_NAME)
client = make_client()
custom_identifier = make_custom_identifier(EXAMPLE_NAME)
respan = make_respan(EXAMPLE_NAME, custom_identifier)
text = ""

try:
with example_attributes(EXAMPLE_NAME, custom_identifier):
print_start(EXAMPLE_NAME, custom_identifier)
text = _rerank_workflow(client)
text = _rerank_workflow("Which document is about observability?")
finally:
respan.shutdown()

Expand Down
Loading