Repository navigation
Expand file tree
/
Copy pathtypes.py
More file actions
79 lines (61 loc) · 2.14 KB
/
Copy pathtypes.py
File metadata and controls
79 lines (61 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
from abc import ABC, abstractmethod
from enum import Enum
from typing import (
Any,
AsyncGenerator,
Generator,
Generic,
List,
Protocol,
Type,
TypeVar,
Union,
runtime_checkable,
)
from llama_index.bridge.pydantic import BaseModel
from llama_index.llms.base import ChatMessage, MessageRole
Model = TypeVar("Model", bound=BaseModel)
TokenGen = Generator[str, None, None]
TokenAsyncGen = AsyncGenerator[str, None]
RESPONSE_TEXT_TYPE = Union[BaseModel, str, TokenGen]
# TODO: move into a `core` folder
# NOTE: this is necessary to make it compatible with pydantic
@runtime_checkable
class BaseOutputParser(Protocol):
"""Output parser class."""
@abstractmethod
def parse(self, output: str) -> Any:
"""Parse, validate, and correct errors programmatically."""
@abstractmethod
def format(self, query: str) -> str:
"""Format a query with structured output formatting instructions."""
def format_messages(self, messages: List[ChatMessage]) -> List[ChatMessage]:
"""Format a list of messages with structured output formatting instructions."""
# NOTE: apply output parser to either the first message if it's a system message
# or the last message
if messages:
if messages[0].role == MessageRole.SYSTEM:
messages[0].content = self.format(messages[0].content or "")
else:
messages[-1].content = self.format(messages[-1].content or "")
return messages
class BasePydanticProgram(ABC, Generic[Model]):
"""A base class for LLM-powered function that return a pydantic model.
Note: this interface is not yet stable.
"""
@property
@abstractmethod
def output_cls(self) -> Type[Model]:
pass
@abstractmethod
def __call__(self, *args: Any, **kwds: Any) -> Model:
pass
async def acall(self, *args: Any, **kwds: Any) -> Model:
return self(*args, **kwds)
class PydanticProgramMode(str, Enum):
"""Pydantic program mode."""
DEFAULT = "default"
OPENAI = "openai"
LLM = "llm"
GUIDANCE = "guidance"
LM_FORMAT_ENFORCER = "lm-format-enforcer"