Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
64 changes: 64 additions & 0 deletions examples/code_interpreter_demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
from together import Together

client = Together()

# Create a code interpreter instance
code_interpreter = client.code_interpreter

# Example 1: Simple print statement
print("Example 1: Simple print")
response = code_interpreter.run(
code='print("Hello from Together!")',
language="python"
)
print(f"Status: {response.data.status}")
for output in response.data.outputs:
print(f"{output.type}: {output.data}")
if response.data.errors:
print(f"Errors: {response.data.errors}")
print("\n")

# Example 2: Using session for maintaining state
print("Example 2: Using session for state")
response1 = code_interpreter.run(
code='x = 42',
language="python"
)
session_id = response1.data.session_id

response2 = code_interpreter.run(
code='print(f"The value of x is {x}")',
language="python",
session_id=session_id
)
for output in response2.data.outputs:
print(f"{output.type}: {output.data}")
if response2.data.errors:
print(f"Errors: {response2.data.errors}")
print("\n")

# Example 3: More complex computation
print("Example 3: Complex computation")
code = '''
!pip install numpy
import numpy as np

# Create a random matrix
matrix = np.random.rand(3, 3)
print("Random matrix:")
print(matrix)

# Calculate eigenvalues
eigenvalues = np.linalg.eigvals(matrix)
print("\\nEigenvalues:")
print(eigenvalues)
'''

response = code_interpreter.run(
code=code,
language="python"
)
for output in response.data.outputs:
print(f"{output.type}: {output.data}")
if response.data.errors:
print(f"Errors: {response.data.errors}")
5 changes: 5 additions & 0 deletions src/together/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from together import resources
from together.constants import BASE_URL, MAX_RETRIES, TIMEOUT_SECS
from together.error import AuthenticationError
from together.resources.code_interpreter import CodeInterpreter
from together.types import TogetherClient
from together.utils import enforce_trailing_slash

Expand All @@ -20,6 +21,7 @@ class Together:
fine_tuning: resources.FineTuning
rerank: resources.Rerank
audio: resources.Audio
code_interpreter: CodeInterpreter

# client options
client: TogetherClient
Expand Down Expand Up @@ -82,6 +84,7 @@ def __init__(
self.rerank = resources.Rerank(self.client)
self.audio = resources.Audio(self.client)
self.endpoints = resources.Endpoints(self.client)
self.code_interpreter = CodeInterpreter(self.client)


class AsyncTogether:
Expand All @@ -93,6 +96,7 @@ class AsyncTogether:
models: resources.AsyncModels
fine_tuning: resources.AsyncFineTuning
rerank: resources.AsyncRerank
code_interpreter: CodeInterpreter

# client options
client: TogetherClient
Expand Down Expand Up @@ -153,6 +157,7 @@ def __init__(
self.models = resources.AsyncModels(self.client)
self.fine_tuning = resources.AsyncFineTuning(self.client)
self.rerank = resources.AsyncRerank(self.client)
self.code_interpreter = CodeInterpreter(self.client)


Client = Together
Expand Down
2 changes: 1 addition & 1 deletion src/together/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
MAX_RETRY_DELAY = 8.0

# API defaults
BASE_URL = "https://api.together.xyz/v1"
BASE_URL = "https://api.together.xyz"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this breaks openai python sdk compatibility - the version needs to be in the base url

could you add a redirect for /v1/tci -> /tci to fix this?


# Download defaults
DOWNLOAD_BLOCK_SIZE = 10 * 1024 * 1024 # 10 MB
Expand Down
4 changes: 2 additions & 2 deletions src/together/resources/audio/speech.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def create(
response, streamed, _ = requestor.request(
options=TogetherRequest(
method="POST",
url="audio/speech",
url="/v1/audio/speech",
params=parameter_payload,
),
stream=stream,
Expand Down Expand Up @@ -144,7 +144,7 @@ async def create(
response, _, _ = await requestor.arequest(
options=TogetherRequest(
method="POST",
url="audio/speech",
url="/v1/audio/speech",
params=parameter_payload,
),
stream=stream,
Expand Down
4 changes: 2 additions & 2 deletions src/together/resources/chat/completions.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def create(
response, _, _ = requestor.request(
options=TogetherRequest(
method="POST",
url="chat/completions",
url="/v1/chat/completions",
params=parameter_payload,
),
stream=stream,
Expand Down Expand Up @@ -283,7 +283,7 @@ async def create(
response, _, _ = await requestor.arequest(
options=TogetherRequest(
method="POST",
url="chat/completions",
url="/v1/chat/completions",
params=parameter_payload,
),
stream=stream,
Expand Down
58 changes: 58 additions & 0 deletions src/together/resources/code_interpreter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
from __future__ import annotations

from typing import Dict, Literal, Optional

from together.abstract import api_requestor
from together.together_response import TogetherResponse
from together.types import TogetherClient, TogetherRequest
from together.types.code_interpreter import ExecuteResponse


class CodeInterpreter:
"""Code Interpreter resource for executing code snippets."""

def __init__(self, client: TogetherClient) -> None:
self._client = client

def run(
self,
code: str,
language: Literal["python"],
session_id: Optional[str] = None,
) -> ExecuteResponse:
"""Execute a code snippet.

Args:
code (str): Code snippet to execute
language (str): Programming language for the code to execute. Currently only supports Python.
session_id (str, optional): Identifier of the current session. Used to make follow-up calls.

Returns:
ExecuteResponse: Object containing execution results and outputs
"""
requestor = api_requestor.APIRequestor(
client=self._client,
)

data: Dict[str, str] = {
"code": code,
"language": language,
}

if session_id is not None:
data["session_id"] = session_id

# Use absolute URL to bypass the /v1 prefix
response, _, _ = requestor.request(
options=TogetherRequest(
method="POST",
url="/tci/execute",
params=data,
),
stream=False,
)

assert isinstance(response, TogetherResponse)

# Return the response data directly since our types match the API structure
return ExecuteResponse(**response.data)
4 changes: 2 additions & 2 deletions src/together/resources/completions.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def create(
response, _, _ = requestor.request(
options=TogetherRequest(
method="POST",
url="completions",
url="/v1/completions",
params=parameter_payload,
),
stream=stream,
Expand Down Expand Up @@ -247,7 +247,7 @@ async def create(
response, _, _ = await requestor.arequest(
options=TogetherRequest(
method="POST",
url="completions",
url="/v1/completions",
params=parameter_payload,
),
stream=stream,
Expand Down
4 changes: 2 additions & 2 deletions src/together/resources/embeddings.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def create(
response, _, _ = requestor.request(
options=TogetherRequest(
method="POST",
url="embeddings",
url="/v1/embeddings",
params=parameter_payload,
),
stream=False,
Expand Down Expand Up @@ -93,7 +93,7 @@ async def create(
response, _, _ = await requestor.arequest(
options=TogetherRequest(
method="POST",
url="embeddings",
url="/v1/embeddings",
params=parameter_payload,
),
stream=False,
Expand Down
24 changes: 12 additions & 12 deletions src/together/resources/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def list(
response, _, _ = requestor.request(
options=TogetherRequest(
method="GET",
url="endpoints",
url="/v1/endpoints",
params=params,
),
stream=False,
Expand Down Expand Up @@ -98,7 +98,7 @@ def create(
response, _, _ = requestor.request(
options=TogetherRequest(
method="POST",
url="endpoints",
url="/v1/endpoints",
params=data,
),
stream=False,
Expand All @@ -125,7 +125,7 @@ def get(self, endpoint_id: str) -> DedicatedEndpoint:
response, _, _ = requestor.request(
options=TogetherRequest(
method="GET",
url=f"endpoints/{endpoint_id}",
url=f"/v1/endpoints/{endpoint_id}",
),
stream=False,
)
Expand All @@ -148,7 +148,7 @@ def delete(self, endpoint_id: str) -> None:
requestor.request(
options=TogetherRequest(
method="DELETE",
url=f"endpoints/{endpoint_id}",
url=f"/v1/endpoints/{endpoint_id}",
),
stream=False,
)
Expand Down Expand Up @@ -203,7 +203,7 @@ def update(
response, _, _ = requestor.request(
options=TogetherRequest(
method="PATCH",
url=f"endpoints/{endpoint_id}",
url=f"/v1/endpoints/{endpoint_id}",
params=data,
),
stream=False,
Expand Down Expand Up @@ -235,7 +235,7 @@ def list_hardware(self, model: Optional[str] = None) -> List[HardwareWithStatus]
response, _, _ = requestor.request(
options=TogetherRequest(
method="GET",
url="hardware",
url="/v1/hardware",
params=params,
),
stream=False,
Expand Down Expand Up @@ -275,7 +275,7 @@ async def list(
response, _, _ = await requestor.arequest(
options=TogetherRequest(
method="GET",
url="endpoints",
url="/v1/endpoints",
params=params,
),
stream=False,
Expand Down Expand Up @@ -336,7 +336,7 @@ async def create(
response, _, _ = await requestor.arequest(
options=TogetherRequest(
method="POST",
url="endpoints",
url="/v1/endpoints",
params=data,
),
stream=False,
Expand All @@ -363,7 +363,7 @@ async def get(self, endpoint_id: str) -> DedicatedEndpoint:
response, _, _ = await requestor.arequest(
options=TogetherRequest(
method="GET",
url=f"endpoints/{endpoint_id}",
url=f"/v1/endpoints/{endpoint_id}",
),
stream=False,
)
Expand All @@ -386,7 +386,7 @@ async def delete(self, endpoint_id: str) -> None:
await requestor.arequest(
options=TogetherRequest(
method="DELETE",
url=f"endpoints/{endpoint_id}",
url=f"/v1/endpoints/{endpoint_id}",
),
stream=False,
)
Expand Down Expand Up @@ -441,7 +441,7 @@ async def update(
response, _, _ = await requestor.arequest(
options=TogetherRequest(
method="PATCH",
url=f"endpoints/{endpoint_id}",
url=f"/v1/endpoints/{endpoint_id}",
params=data,
),
stream=False,
Expand Down Expand Up @@ -475,7 +475,7 @@ async def list_hardware(
response, _, _ = await requestor.arequest(
options=TogetherRequest(
method="GET",
url="hardware",
url="/v1/hardware",
params=params,
),
stream=False,
Expand Down
Loading
Loading