Python client to interact with Aleph Alpha API endpoints
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Create and use steering concepts to guide model behavior and output style. Steering allows fine-grained control over how models generate content by defining positive and negative examples.
Define positive and negative examples to teach the model desired behavior patterns.
class SteeringPairedExample:
negative: str
positive: str
def __init__(self, negative: str, positive: str):
"""
Paired example showing negative and positive versions of content.
Parameters:
- negative: Example of undesired style or content
- positive: Example of desired style or content
"""
def to_json(self) -> Mapping[str, Any]:
"""Convert example to JSON format."""Create steering concepts from paired examples that can be applied to completion requests.
class SteeringConceptCreationRequest:
examples: List[SteeringPairedExample]
def __init__(self, examples: List[SteeringPairedExample]):
"""
Creates a new steering concept that can be used in completion requests.
Parameters:
- examples: A list of SteeringPairedExample objects showing desired transformations
"""
def to_json(self) -> Mapping[str, Any]:
"""Convert request to JSON format."""Response containing the created steering concept identifier.
class SteeringConceptCreationResponse:
id: str
def __init__(self, id: str):
"""
Response from steering concept creation.
Attributes:
- id: Unique identifier for the created steering concept
"""
@staticmethod
def from_json(json: Dict[str, Any]) -> SteeringConceptCreationResponse:
"""Create response from JSON data."""Create and manage steering concepts using synchronous and asynchronous clients.
def create_steering_concept(
self,
request: SteeringConceptCreationRequest
) -> SteeringConceptCreationResponse:
"""
Create a new steering concept.
Parameters:
- request: Steering concept configuration with examples
Returns:
SteeringConceptCreationResponse with concept ID
"""
async def create_steering_concept(
self,
request: SteeringConceptCreationRequest
) -> SteeringConceptCreationResponse:
"""
Create a new steering concept (async).
Parameters:
- request: Steering concept configuration with examples
Returns:
SteeringConceptCreationResponse with concept ID
"""Creating and using steering concepts for content control:
from aleph_alpha_client import (
Client,
SteeringPairedExample,
SteeringConceptCreationRequest,
CompletionRequest,
Prompt
)
client = Client(token="your-api-token")
# Create steering examples for casual vs formal language
examples = [
SteeringPairedExample(
negative="I appreciate your valuable feedback on this matter.",
positive="Thanks for the real talk, fam."
),
SteeringPairedExample(
negative="The financial projections indicate significant growth potential.",
positive="Yo, these numbers are looking mad stacked!"
),
SteeringPairedExample(
negative="Please consider attending our upcoming meeting.",
positive="Come hang out at our meetup if you're free!"
),
SteeringPairedExample(
negative="We should proceed with caution regarding this decision.",
positive="Let's not rush into this, just saying."
)
]
# Create steering concept
steering_request = SteeringConceptCreationRequest(examples=examples)
steering_response = client.create_steering_concept(steering_request)
print(f"Created steering concept: {steering_response.id}")
# Use steering concept in completion
completion_request = CompletionRequest(
prompt=Prompt.from_text("Write a professional email about project delays"),
maximum_tokens=150,
temperature=0.7,
steering_concepts=[steering_response.id] # Apply casual style steering
)
response = client.complete(completion_request, model="luminous-extended")
print("Steered completion:")
print(response.completions[0].completion)
# Create steering for technical explanation style
tech_examples = [
SteeringPairedExample(
negative="Machine learning is when computers learn things automatically.",
positive="Machine learning algorithms optimize parameters through iterative training on labeled datasets to minimize loss functions."
),
SteeringPairedExample(
negative="AI helps solve problems by being smart.",
positive="Artificial intelligence systems utilize pattern recognition, statistical inference, and computational optimization to process complex data."
),
SteeringPairedExample(
negative="The program runs faster now.",
positive="The optimized algorithm demonstrates improved computational complexity from O(n²) to O(n log n)."
)
]
tech_steering_request = SteeringConceptCreationRequest(examples=tech_examples)
tech_steering_response = client.create_steering_concept(tech_steering_request)
# Apply technical steering
tech_completion = CompletionRequest(
prompt=Prompt.from_text("Explain how neural networks work"),
maximum_tokens=200,
temperature=0.3,
steering_concepts=[tech_steering_response.id]
)
tech_response = client.complete(tech_completion, model="luminous-extended")
print("Technical explanation:")
print(tech_response.completions[0].completion)
# Combine multiple steering concepts
mixed_completion = CompletionRequest(
prompt=Prompt.from_text("Describe the benefits of cloud computing for small businesses"),
maximum_tokens=180,
steering_concepts=[
steering_response.id, # Casual tone
tech_steering_response.id # Technical depth
]
)
mixed_response = client.complete(mixed_completion, model="luminous-extended")
print("Multi-steered completion (casual + technical):")
print(mixed_response.completions[0].completion)
# Async steering concept creation
import asyncio
async def create_async_steering():
async with AsyncClient(token="your-api-token") as client:
examples = [
SteeringPairedExample(
negative="This is a good solution to the problem.",
positive="This solution elegantly addresses the core requirements while maintaining scalability."
)
]
request = SteeringConceptCreationRequest(examples=examples)
response = await client.create_steering_concept(request)
return response.id
concept_id = asyncio.run(create_async_steering())
print(f"Async created concept: {concept_id}")Install with Tessl CLI
npx tessl i tessl/pypi-aleph-alpha-client