Views
No views yet
1from llmpromptkit import PromptManager, PromptTesting
2from llmpromptkit.integrations.huggingface import get_huggingface_callback
3
4# Initialize components
5prompt_manager = PromptManager()
6testing = PromptTesting(prompt_manager)
7
8# Get a HuggingFace callback
9hf_callback = get_huggingface_callback(
10 model_name="google/flan-t5-base",
11 task="text2text-generation"
12)
13
14# Run tests with the model
15test_results = await testing.run_test_cases(prompt_id="your_prompt_id", llm_callback=hf_callback)1pip install llmpromptkit
2
3Quick Start
4
5from llmpromptkit import PromptManager, VersionControl, PromptTesting, Evaluator
6
7# Initialize components
8prompt_manager = PromptManager()
9version_control = VersionControl(prompt_manager)
10testing = PromptTesting(prompt_manager)
11evaluator = Evaluator(prompt_manager)
12
13# Create a prompt
14prompt = prompt_manager.create(
15 content="Summarize the following text: {text}",
16 name="Simple Summarization",
17 description="A simple prompt for text summarization",
18 tags=["summarization", "basic"]
19)
20
21# Create a new version
22version_control.commit(
23 prompt_id=prompt.id,
24 commit_message="Initial version"
25)
26
27# Update the prompt
28prompt_manager.update(
29 prompt.id,
30 content="Please provide a concise summary of the following text in 2-3 sentences: {text}"
31)
32
33# Commit the updated version
34version_control.commit(
35 prompt_id=prompt.id,
36 commit_message="Improved prompt with length guidance"
37)
38
39# Create a test case
40test_case = testing.create_test_case(
41 prompt_id=prompt.id,
42 input_vars={"text": "Lorem ipsum dolor sit amet..."},
43 expected_output="This is a summary of the text."
44)
45
46# Define an LLM callback for testing
47async def llm_callback(prompt, vars):
48 # In a real scenario, this would call an actual LLM API
49 return "This is a summary of the text."
50
51# Run the test case
52import asyncio
53test_result = asyncio.run(testing.run_test_case(
54 test_case_id=test_case.id,
55 llm_callback=llm_callback
56))
57
58# Evaluate a prompt with multiple inputs
59evaluation_result = asyncio.run(evaluator.evaluate_prompt(
60 prompt_id=prompt.id,
61 inputs=[{"text": "Sample text 1"}, {"text": "Sample text 2"}],
62 llm_callback=llm_callback
63))
64
65print(f"Evaluation metrics: {evaluation_result['aggregated_metrics']}")
66
67Command-line Interface
68LLMPromptKit comes with a powerful CLI for managing prompts:
69
70# Create a prompt
71llmpromptkit prompt create "Summarization" --content "Summarize: {text}" --tags "summarization,basic"
72
73# List all prompts
74llmpromptkit prompt list
75
76# Create a new version
77llmpromptkit version commit <prompt_id> --message "Updated prompt"
78
79# Run tests
80llmpromptkit test run-all <prompt_id> --llm openai
81
82Advanced Usage
83Advanced Templating
84LLMPromptKit supports advanced templating with conditionals and loops:
85
86from llmpromptkit import PromptTemplate
87
88template = PromptTemplate("""
89{system_message}
90
91{for example in examples}
92Input: {example.input}
93Output: {example.output}
94{endfor}
95
96Input: {input}
97Output:
98""")
99
100rendered = template.render(
101 system_message="You are a helpful assistant.",
102 examples=[
103 {"input": "Hello", "output": "Hi there!"},
104 {"input": "How are you?", "output": "I'm doing well, thanks!"}
105 ],
106 input="What's the weather like?"
107)
108
109Custom Evaluation Metrics
110Create custom metrics to evaluate prompt performance:
111from llmpromptkit import EvaluationMetric, Evaluator
112
113class CustomMetric(EvaluationMetric):
114 def __init__(self):
115 super().__init__("custom_metric", "My custom evaluation metric")
116
117 def compute(self, generated_output, expected_output=None, **kwargs):
118 # Custom logic to score the output
119 return score # A float between 0 and 1
120
121# Register the custom metric
122evaluator = Evaluator(prompt_manager)
123evaluator.register_metric(CustomMetric())
124
125Use Cases
126
127Prompt Development: Iteratively develop and refine prompts with version control
128Prompt Optimization: A/B test different prompt variations to find the most effective approach
129Quality Assurance: Ensure prompt quality with automated testing and evaluation
130Team Collaboration: Share and collaborate on prompts with a centralized management system
131Production Deployment: Maintain consistent prompt quality in production applications
132
133License
134MIT License
135
136## Contributing
137Contributions are welcome! Please feel free to submit a Pull Request.
138
139## Author
140Biswanath Roul - [GitHub](https://github.com/biswanathroul)
141