Skip to content
Merged
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
10 changes: 5 additions & 5 deletions examples/anthropic/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ This directory contains examples of using Anthropic's Claude API with environmen

```bash
# Basic Chat
python anthropic-chat.py --model "claude-3-sonnet-latest" --message "What is machine learning?" --max-tokens 100
uv run python -m examples.anthropic.anthropic-chat --model "claude-3-opus-latest" --message "What is machine learning?" --max-tokens 100

# With custom API key
python anthropic-chat.py --message "Explain neural networks" --api-key "your-api-key"
# Async Chat
uv run python -m examples.anthropic.anthropic-async-chat --model "claude-3-opus-latest" --message "What is machine learning?" --max-tokens 100

# Streaming Chat
python anthropic-stream-chat.py --message "Write a story about AI" --model "claude-3-opus-latest"
uv run python -m examples.anthropic.anthropic-stream-chat --model "claude-3-opus-latest" --message "Write a story about AI"

# Async streaming
python anthropic-async-stream-chat.py --message "Explain quantum mechanics" --max-tokens 200
uv run python -m examples.anthropic.anthropic-async-stream-chat --model "claude-3-opus-latest" --message "Explain quantum mechanics" --max-tokens 200
```
2 changes: 1 addition & 1 deletion examples/anthropic/anthropic-chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def main(model: str, message: str, max_tokens: int, api_key: str | None = None):
parser.add_argument(
"--model",
type=str,
default="claude-3-5-sonnet-latest",
default="claude-3-5-sonnet-20241022",
help="Model to use for chat completion",
)
parser.add_argument(
Expand Down
20 changes: 14 additions & 6 deletions examples/cohere/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,22 @@ This directory contains examples of using Cohere's API with environmental impact

### Basic Chat (v1)
```bash
python cohere-chat.py --message "What is machine learning?" --max-tokens 100
uv run python -m examples.cohere.cohere-chat --message "What is machine learning?" --max-tokens 100

# With custom API key
python cohere-chat.py --message "Explain neural networks" --api-key "your-api-key"
uv run python -m examples.cohere.cohere-async-chat --message "Explain neural networks" --max-tokens 200

uv run python -m examples.cohere.cohere-stream-chat --message "Write a story about AI"

python cohere_v2-stream-chat.py --model "command-r-plus-08-2024" --message "Write a story about AI"
uv run python -m examples.cohere.cohere-async-stream-chat --message "Explain quantum mechanics" --max-tokens 200
```

# Async streaming
python cohere_v2-async-stream-chat.py --message "Explain quantum mechanics" --max-tokens 200
### Basic Chat (v2)
```bash
uv run python -m examples.cohere.cohere_v2-chat --message "What is machine learning?" --max-tokens 100

uv run python -m examples.cohere.cohere_v2-async-chat --message "Explain neural networks" --max-tokens 200

uv run python -m examples.cohere.cohere_v2-stream-chat --message "Write a story about AI"

uv run python -m examples.cohere.cohere_v2-async-stream-chat --message "Explain quantum mechanics" --max-tokens 200
```
12 changes: 9 additions & 3 deletions examples/cohere/cohere-async-chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
from scope3ai import Scope3AI


async def main(message: str, max_tokens: int, api_key: str | None = None):
async def main(message: str, model: str, max_tokens: int, api_key: str | None = None):
scope3 = Scope3AI.init()
co = cohere.Client(api_key=api_key) if api_key else cohere.Client()
co = cohere.AsyncClient(api_key=api_key) if api_key else cohere.AsyncClient()

with scope3.trace() as tracer:
response = await co.chat(message=message, max_tokens=max_tokens)
response = await co.chat(message=message, model=model, max_tokens=max_tokens)
print(response)

impact = tracer.impact()
Expand All @@ -35,6 +35,12 @@ async def main(message: str, max_tokens: int, api_key: str | None = None):
default=100,
help="Maximum number of tokens in the response",
)
parser.add_argument(
"--model",
type=str,
default="command-r",
help="Model to use for the chat",
)
parser.add_argument(
"--api-key",
type=str,
Expand Down
13 changes: 10 additions & 3 deletions examples/cohere/cohere-async-stream-chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
from scope3ai import Scope3AI


async def main(message: str, max_tokens: int, api_key: str | None = None):
async def main(message: str, model: str, max_tokens: int, api_key: str | None = None):
scope3 = Scope3AI.init()
co = cohere.AsyncClient(api_key=api_key) if api_key else cohere.AsyncClient()

with scope3.trace() as tracer:
stream = co.chat_stream(message=message, max_tokens=max_tokens)
stream = co.chat_stream(message=message, model=model, max_tokens=max_tokens)
async for event in stream:
print(event)
if event.event_type == "text-generation":
print(event.text, end="", flush=True)

impact = await tracer.aimpact()
print(f"Total Energy Wh: {impact.total_energy_wh}")
Expand All @@ -38,6 +39,12 @@ async def main(message: str, max_tokens: int, api_key: str | None = None):
default=100,
help="Maximum number of tokens in the response",
)
parser.add_argument(
"--model",
type=str,
default="command-r",
help="Model to use for the chat",
)
parser.add_argument(
"--api-key",
type=str,
Expand Down
10 changes: 8 additions & 2 deletions examples/cohere/cohere-chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
from scope3ai import Scope3AI


def main(message: str, max_tokens: int, api_key: str | None = None):
def main(message: str, model: str, max_tokens: int, api_key: str | None = None):
scope3 = Scope3AI.init()
co = cohere.Client(api_key=api_key) if api_key else cohere.Client()

with scope3.trace() as tracer:
response = co.chat(message=message, max_tokens=max_tokens)
response = co.chat(message=message, model=model, max_tokens=max_tokens)
print(response)

impact = tracer.impact()
Expand All @@ -28,6 +28,12 @@ def main(message: str, max_tokens: int, api_key: str | None = None):
default="Hello!",
help="Message to send to the chat model",
)
parser.add_argument(
"--model",
type=str,
default="command-r",
help="Model to use for the chat",
)
parser.add_argument(
"--max-tokens",
type=int,
Expand Down
3 changes: 2 additions & 1 deletion examples/cohere/cohere-stream-chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ def main(message: str, max_tokens: int, api_key: str | None = None):
with scope3.trace() as tracer:
stream = co.chat_stream(message=message, max_tokens=max_tokens)
for event in stream:
print(event)
if event.event_type == "text-generation":
print(event.text, end="", flush=True)

impact = tracer.impact()
print(f"Total Energy Wh: {impact.total_energy_wh}")
Expand Down
11 changes: 10 additions & 1 deletion examples/cohere/cohere_v2-async-chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@
from scope3ai import Scope3AI


async def main(model: str, message: str, role: str, api_key: str | None = None):
async def main(
model: str, message: str, role: str, max_tokens: int, api_key: str | None = None
):
scope3 = Scope3AI.init()
co = cohere.AsyncClientV2(api_key=api_key) if api_key else cohere.AsyncClientV2()

with scope3.trace() as tracer:
response = await co.chat(
model=model,
messages=[{"role": role, "content": message}],
max_tokens=max_tokens,
)
print(response)

Expand Down Expand Up @@ -40,6 +43,12 @@ async def main(model: str, message: str, role: str, api_key: str | None = None):
default="Hello world!",
help="Message to send to the chat model",
)
parser.add_argument(
"--max-tokens",
type=int,
default=100,
help="Maximum number of tokens in the response",
)
parser.add_argument("--role", type=str, default="user", help="Role for the message")
parser.add_argument(
"--api-key",
Expand Down
8 changes: 7 additions & 1 deletion examples/cohere/cohere_v2-async-stream-chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,14 @@ async def main(
messages=[{"role": role, "content": message}],
max_tokens=max_tokens,
)

async for event in stream:
print(event)
if (
hasattr(event, "delta")
and hasattr(event.delta, "message")
and event.delta.message.content
):
print(event.delta.message.content.text, end="", flush=True)

impact = await tracer.aimpact()
print(f"Total Energy Wh: {impact.total_energy_wh}")
Expand Down
11 changes: 10 additions & 1 deletion examples/cohere/cohere_v2-chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@
from scope3ai import Scope3AI


def main(model: str, message: str, role: str, api_key: str | None = None):
def main(
model: str, message: str, role: str, max_tokens: int, api_key: str | None = None
):
scope3 = Scope3AI.init()
co = cohere.ClientV2(api_key=api_key) if api_key else cohere.ClientV2()

with scope3.trace() as tracer:
response = co.chat(
model=model,
messages=[{"role": role, "content": message}],
max_tokens=max_tokens,
)
print(response)

Expand All @@ -31,6 +34,12 @@ def main(model: str, message: str, role: str, api_key: str | None = None):
default="command-r-plus-08-2024",
help="Model to use for chat completion",
)
parser.add_argument(
"--max-tokens",
type=int,
default=100,
help="Maximum number of tokens in the response",
)
parser.add_argument(
"--message",
type=str,
Expand Down
7 changes: 6 additions & 1 deletion examples/cohere/cohere_v2-stream-chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ def main(
max_tokens=max_tokens,
)
for event in stream:
print(event)
if (
hasattr(event, "delta")
and hasattr(event.delta, "message")
and event.delta.message.content
):
print(event.delta.message.content.text, end="", flush=True)

impact = tracer.impact()
print(f"Total Energy Wh: {impact.total_energy_wh}")
Expand Down
26 changes: 20 additions & 6 deletions examples/hugging-face/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,31 @@ This directory contains examples of using HuggingFace's Inference API with envir

```bash
# chat
python chat.py --message "Explain the theory of relativity" --max-tokens 100
uv run python -m examples.hugging-face.chat --message "Explain the theory of relativity" --max-tokens 100

# chat async
python chat-async.py --message "What is quantum computing?"
uv run python -m examples.hugging-face.chat-async --message "What is quantum computing?"

# speech & Audio
python speech-to-text.py --audio-path "recording.wav"
uv run python -m examples.hugging-face.speech-to-text --audio-path "recording.wav"

# speech & Audio async
python text-to-speech.py --text "Hello world!"
uv run python -m examples.hugging-face.text-to-speech --text "Hello world!"

# translation
python translation.py --text "Hello, how are you?" --target-language "es"
```
uv run python -m examples.hugging-face.translation --text "Hello, how are you?" --target-language "es"

# text to image
uv run python -m examples.hugging-face.text-to-image --prompt "A beautiful sunset over mountains" --model "dall-e-2" --size "1024x1024"

# text to image async
uv run python -m examples.hugging-face.text-to-image-async --prompt "A futuristic city" --model "dall-e-2" --size "1024x1024"

# image to image
uv run python -m examples.hugging-face.image-to-image --image-path "image.png" --prompt "A beautiful sunset over mountains" --model "dall-e-2" --size "1024x1024"

# image to image async
uv run python -m examples.hugging-face.image-to-image-async --image-path "image.png" --prompt "A futuristic city" --model "dall-e-2" --size "1024x1024"


```
16 changes: 11 additions & 5 deletions examples/litellm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,19 @@ This directory contains examples of using LiteLLM with environmental impact trac

### Completion
```bash
python litellm-completion.py --model "gpt-4" --message "What is artificial intelligence?"
uv run python -m examples.litellm.litellm-completion --model "gpt-4" --message "What is artificial intelligence?"

python litellm-acompletion.py --message "Explain quantum computing" --max-tokens 200
uv run python -m examples.litellm.litellm-acompletion --model "gpt-4" --message "Explain quantum computing" --max-tokens 200

python litellm-image-generation.py --prompt "A beautiful sunset" --size "1024x1024"
uv run python -m examples.litellm.litellm-image-generation --prompt "A beautiful sunset" --size "1024x1024"

python litellm-speech.py --text "Hello world" --voice "alloy"
uv run python -m examples.litellm.litellm-aimage-generation --prompt "A beautiful sunset" --size "1024x1024"

python litellm-transcription.py --audio-path "recording.wav" --model "whisper-1"
uv run python -m examples.litellm.litellm-speech --text "Hello world" --voice "alloy"

uv run python -m examples.litellm.litellm-aspeech --text "Hello world" --voice "alloy"

uv run python -m examples.litellm.litellm-transcription --audio-path "recording.wav" --model "whisper-1"

uv run python -m examples.litellm.litellm-atranscription --audio-path "recording.wav" --model "whisper-1"
```
1 change: 0 additions & 1 deletion examples/litellm/litellm-aimage-generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

async def main(model: str, prompt: str, size: str, api_key: str | None = None):
scope3 = Scope3AI.init()

with scope3.trace() as tracer:
response = await aimage_generation(
model=model, prompt=prompt, size=size, api_key=api_key
Expand Down
Loading