/ src / evidently / llm / datagen / config.py
config.py
 1  import random
 2  from typing import Any
 3  from typing import List
 4  from typing import Optional
 5  from typing import Union
 6  
 7  from evidently.llm.utils.blocks import PromptBlock
 8  
 9  
10  class UserProfile(PromptBlock):
11      """Assume the user profile is:
12      - Intent: {intent}
13      - Role: {role}
14      - Tone: {tone}"""
15  
16      intent: Optional[str] = None
17      """Optional user intent or goal."""
18      role: Optional[str] = None
19      """Optional user role or persona."""
20      tone: str = "neutral"
21      """Communication tone (default: "neutral")."""
22  
23      def render(self) -> str:
24          messages = ["Assume the user profile is:"]
25          if self.intent is not None:
26              messages.append(f"- Intent: {self.intent}")
27          if self.role is not None:
28              messages.append(f"- Role: {self.role}")
29          messages.append(f"- Tone: {self.tone}")
30          return "\n".join(messages)
31  
32  
33  class Examples(PromptBlock):
34      """Use these examples as stylistic guidance:
35      {examples}"""
36  
37      examples: List[str] = []
38      """List of example strings to use as stylistic guidance."""
39  
40      def render(self) -> str:
41          res = ["Use these examples as stylistic guidance:"]
42          for ex in self.examples:
43              res.append(f"- {ex}")
44          return "\n".join(res)
45  
46      def choice(self):
47          return random.choice(self.examples)
48  
49  
50  class ServiceSpec(PromptBlock):
51      """Service: {kind}
52      Purpose: {purpose}"""
53  
54      kind: str
55      """Type of service (e.g., "RAG", "chatbot")."""
56      purpose: str = ""
57      """Description of the service's purpose."""
58  
59      def __init__(self, kind: str, purpose: str = "", **data: Any):
60          self.kind = kind
61          self.purpose = purpose
62          super().__init__(**data)
63  
64  
65  class GenerationSpec(PromptBlock):
66      """Generate {kind} with {complexity} complexity.
67      {examples}"""
68  
69      kind: str = "questions"
70      """Type of content to generate (e.g., "questions", "responses")."""
71      complexity: str = "medium"
72      """Complexity level ("low", "medium", "high")."""
73      examples: Optional[Examples] = None
74      """Optional examples to guide generation."""
75  
76      def __init__(
77          self, kind: str, complexity: str = "medium", examples: Union[Examples, List[str], str, None] = None, **data: Any
78      ):
79          self.kind = kind
80          self.complexity = complexity
81          if isinstance(examples, str):
82              examples = [examples]
83          if isinstance(examples, list):
84              examples = Examples(examples=examples)
85          self.examples = examples
86          super().__init__(**data)
87  
88      @property
89      def has_examples(self) -> bool:
90          return self.examples is not None and len(self.examples.examples) > 0