Python client for Together's Cloud Platform providing comprehensive AI model APIs
Raw text completion interface for code generation, creative writing, and general text completion tasks. Supports streaming, async operations, and extensive configuration options for fine-tuned control over text generation.
Generate text continuations from a given prompt with configurable generation parameters.
def create(
model: str,
prompt: str,
max_tokens: Optional[int] = None,
temperature: Optional[float] = None,
top_p: Optional[float] = None,
top_k: Optional[int] = None,
repetition_penalty: Optional[float] = None,
stream: bool = False,
logprobs: Optional[int] = None,
echo: Optional[bool] = None,
n: Optional[int] = None,
presence_penalty: Optional[float] = None,
frequency_penalty: Optional[float] = None,
logit_bias: Optional[Dict[str, float]] = None,
stop: Optional[Union[str, List[str]]] = None,
safety_model: Optional[str] = None,
**kwargs
) -> CompletionResponse:
"""
Create a text completion from a prompt.
Args:
model: Model identifier for text completion
prompt: Input text prompt to complete
max_tokens: Maximum tokens to generate
temperature: Sampling temperature (0.0 to 2.0)
top_p: Nucleus sampling probability threshold
top_k: Top-k sampling parameter
repetition_penalty: Penalty for repeating tokens
stream: Enable streaming response chunks
logprobs: Number of log probabilities to return
echo: Include prompt in response
n: Number of completion choices to generate
presence_penalty: Penalty for token presence
frequency_penalty: Penalty for token frequency
logit_bias: Modify likelihood of specific tokens
stop: Stop sequences to end generation
safety_model: Safety model to apply
Returns:
CompletionResponse with generated text
"""Real-time streaming of text completion as it is generated.
def create(
model: str,
prompt: str,
stream: bool = True,
**kwargs
) -> Iterator[CompletionChunk]:
"""
Stream text completion chunks in real-time.
Returns:
Iterator yielding CompletionChunk objects
"""Asynchronous text completion operations for concurrent processing.
async def create(
model: str,
prompt: str,
**kwargs
) -> CompletionResponse:
"""
Asynchronously create text completions.
Returns:
CompletionResponse with generated text
"""from together import Together
client = Together()
response = client.completions.create(
model="codellama/CodeLlama-34b-Python-hf",
prompt="def fibonacci(n):",
max_tokens=200,
temperature=0.1,
stop=["\n\n", "def "]
)
print(response.choices[0].text)response = client.completions.create(
model="meta-llama/Llama-3.2-3B-Instruct-Turbo",
prompt="Once upon a time in a magical forest,",
max_tokens=300,
temperature=0.8,
top_p=0.9
)
print(response.choices[0].text)stream = client.completions.create(
model="codellama/CodeLlama-34b-Python-hf",
prompt="# Python function to calculate prime numbers\ndef is_prime(n):",
stream=True,
max_tokens=150,
temperature=0.2
)
for chunk in stream:
if chunk.choices[0].text:
print(chunk.choices[0].text, end="", flush=True)response = client.completions.create(
model="meta-llama/Llama-3.2-3B-Instruct-Turbo",
prompt="The benefits of renewable energy include:",
max_tokens=100,
temperature=0.7,
n=3 # Generate 3 different completions
)
for i, choice in enumerate(response.choices):
print(f"Completion {i+1}: {choice.text}")import asyncio
from together import AsyncTogether
async def process_completions():
client = AsyncTogether()
prompts = [
"Write a Python function to sort a list:",
"Explain the concept of recursion:",
"Create a simple web scraper in Python:"
]
tasks = [
client.completions.create(
model="codellama/CodeLlama-34b-Python-hf",
prompt=prompt,
max_tokens=200,
temperature=0.3
)
for prompt in prompts
]
responses = await asyncio.gather(*tasks)
for i, response in enumerate(responses):
print(f"Response {i+1}: {response.choices[0].text}")
asyncio.run(process_completions())response = client.completions.create(
model="meta-llama/Llama-3.2-3B-Instruct-Turbo",
prompt="Artificial intelligence will",
max_tokens=150,
temperature=0.7,
top_p=0.9,
top_k=50,
repetition_penalty=1.1,
presence_penalty=0.2,
frequency_penalty=0.1,
stop=[".", "!", "?"],
logprobs=5
)
print(f"Generated text: {response.choices[0].text}")
print(f"Finish reason: {response.choices[0].finish_reason}")
if response.choices[0].logprobs:
print("Token probabilities:")
for token, logprob in zip(
response.choices[0].logprobs.tokens,
response.choices[0].logprobs.token_logprobs
):
print(f" '{token}': {logprob}")class CompletionRequest:
model: str
prompt: str
max_tokens: Optional[int] = None
temperature: Optional[float] = None
top_p: Optional[float] = None
top_k: Optional[int] = None
repetition_penalty: Optional[float] = None
stream: bool = False
logprobs: Optional[int] = None
echo: Optional[bool] = None
n: Optional[int] = None
presence_penalty: Optional[float] = None
frequency_penalty: Optional[float] = None
logit_bias: Optional[Dict[str, float]] = None
stop: Optional[Union[str, List[str]]] = None
safety_model: Optional[str] = Noneclass CompletionResponse:
id: str
object: str
created: int
model: str
choices: List[CompletionChoice]
usage: Usage
class CompletionChoice:
index: int
text: str
finish_reason: Optional[str]
logprobs: Optional[Logprobs]
class Usage:
prompt_tokens: int
completion_tokens: int
total_tokens: int
class Logprobs:
tokens: List[str]
token_logprobs: List[Optional[float]]
top_logprobs: Optional[List[Dict[str, float]]]
text_offset: Optional[List[int]]class CompletionChunk:
id: str
object: str
created: int
model: str
choices: List[CompletionChoiceDelta]
class CompletionChoiceDelta:
index: int
text: str
finish_reason: Optional[str]
logprobs: Optional[Logprobs]Install with Tessl CLI
npx tessl i tessl/pypi-together