Built-in utilities for elicitation, sampling, and context management in Golf v0.2.0.
Golf v0.2.0 introduces powerful built-in utilities that enhance your tool development experience. These utilities are automatically available in all tools without additional imports and provide seamless integration with the Golf Platform for enhanced telemetry and monitoring.
from golf.utils import elicitasync def my_interactive_tool(): # Simple text prompt color = await elicit("What's your favorite color?") # Use the response in your tool logic return {"message": f"Great choice! {color} is a beautiful color."}
from golf.utils import elicitasync def configuration_tool(): # Collect multiple pieces of information name = await elicit("What's your name?") email = await elicit("What's your email address?") preferences = await elicit("Any special preferences? (optional)") # Process and use the collected data config = { "name": name, "email": email, "preferences": preferences or "None specified" } return {"configuration": config}
from golf.utils import elicitasync def age_verification_tool(): while True: age_str = await elicit("Please enter your age:") try: age = int(age_str) if age >= 18: break else: await elicit("You must be 18 or older. Please try again.") except ValueError: await elicit("Please enter a valid number.") return {"verified": True, "age": age}
from golf.utils import sampleasync def explanation_tool(topic: str): # Generate an explanation using the default model explanation = await sample(f"Explain {topic} in simple terms") return {"topic": topic, "explanation": explanation}
from golf.utils import sampleasync def analysis_tool(text: str): # Use a specific model for analysis sentiment = await sample( f"Analyze the sentiment of this text: '{text}'. Respond with only: positive, negative, or neutral.", model="gpt-4" ) summary = await sample( f"Summarize this text in one sentence: '{text}'", model="gpt-3.5-turbo" ) return { "original_text": text, "sentiment": sentiment.strip(), "summary": summary }
from golf.utils import sampleasync def creative_writing_tool(prompt: str, style: str): # Advanced sampling with custom parameters story = await sample( f"Write a short story in {style} style based on: {prompt}", model="gpt-4", max_tokens=500, temperature=0.8 ) return { "prompt": prompt, "style": style, "story": story }