llama-index

Форк
0
109 строк · 4.1 Кб
1
"""
2
LocalAI is a free, open source, and self-hosted OpenAI alternative.
3

4
Docs: https://localai.io/
5
Source: https://github.com/go-skynet/LocalAI
6
"""
7

8
import warnings
9
from types import MappingProxyType
10
from typing import Any, Callable, Dict, Optional, Sequence
11

12
from llama_index.legacy.bridge.pydantic import Field
13
from llama_index.legacy.constants import DEFAULT_CONTEXT_WINDOW
14
from llama_index.legacy.core.llms.types import ChatMessage, LLMMetadata
15
from llama_index.legacy.llms.openai import OpenAI
16
from llama_index.legacy.llms.openai_like import OpenAILike
17
from llama_index.legacy.llms.openai_utils import is_function_calling_model
18
from llama_index.legacy.types import BaseOutputParser, PydanticProgramMode
19

20
# Use these as kwargs for OpenAILike to connect to LocalAIs
21
DEFAULT_LOCALAI_PORT = 8080
22
# TODO: move to MappingProxyType[str, Any] once Python 3.9+
23
LOCALAI_DEFAULTS: Dict[str, Any] = MappingProxyType(  # type: ignore[assignment]
24
    {
25
        "api_key": "localai_fake",
26
        "api_type": "localai_fake",
27
        "api_base": f"http://localhost:{DEFAULT_LOCALAI_PORT}/v1",
28
    }
29
)
30

31

32
class LocalAI(OpenAI):
33
    context_window: int = Field(
34
        default=DEFAULT_CONTEXT_WINDOW,
35
        description="The maximum number of context tokens for the model.",
36
        gt=0,
37
    )
38
    globally_use_chat_completions: Optional[bool] = Field(
39
        default=None,
40
        description=(
41
            "Set None (default) to per-invocation decide on using /chat/completions"
42
            " vs /completions endpoints with query keyword arguments,"
43
            " set False to universally use /completions endpoint,"
44
            " set True to universally use /chat/completions endpoint."
45
        ),
46
    )
47

48
    def __init__(
49
        self,
50
        api_key: Optional[str] = LOCALAI_DEFAULTS["api_key"],
51
        api_base: Optional[str] = LOCALAI_DEFAULTS["api_base"],
52
        system_prompt: Optional[str] = None,
53
        messages_to_prompt: Optional[Callable[[Sequence[ChatMessage]], str]] = None,
54
        completion_to_prompt: Optional[Callable[[str], str]] = None,
55
        pydantic_program_mode: PydanticProgramMode = PydanticProgramMode.DEFAULT,
56
        output_parser: Optional[BaseOutputParser] = None,
57
        **kwargs: Any,
58
    ) -> None:
59
        super().__init__(
60
            api_key=api_key,
61
            api_base=api_base,
62
            system_prompt=system_prompt,
63
            messages_to_prompt=messages_to_prompt,
64
            completion_to_prompt=completion_to_prompt,
65
            pydantic_program_mode=pydantic_program_mode,
66
            output_parser=output_parser,
67
            **kwargs,
68
        )
69
        warnings.warn(
70
            (
71
                f"{type(self).__name__} subclass is deprecated in favor of"
72
                f" {OpenAILike.__name__} composition. The deprecation cycle"
73
                " will complete sometime in late December 2023."
74
            ),
75
            DeprecationWarning,
76
            stacklevel=2,
77
        )
78

79
    @classmethod
80
    def class_name(cls) -> str:
81
        return "LocalAI"
82

83
    @property
84
    def metadata(self) -> LLMMetadata:
85
        return LLMMetadata(
86
            context_window=self.context_window,
87
            num_output=self.max_tokens or -1,
88
            is_chat_model=self._is_chat_model,
89
            is_function_calling_model=is_function_calling_model(
90
                model=self._get_model_name()
91
            ),
92
            model_name=self.model,
93
        )
94

95
    def _update_max_tokens(self, all_kwargs: Dict[str, Any], prompt: str) -> None:
96
        # This subclass only supports max_tokens via LocalAI(..., max_tokens=123)
97
        del all_kwargs, prompt  # Unused
98
        # do nothing
99

100
    @property
101
    def _is_chat_model(self) -> bool:
102
        if self.globally_use_chat_completions is not None:
103
            return self.globally_use_chat_completions
104
        raise NotImplementedError(
105
            "Inferring of when to use /chat/completions is unsupported by"
106
            f" {type(self).__name__}. Please either set 'globally_use_chat_completions'"
107
            " arg during construction, or pass the arg 'use_chat_completions' in your"
108
            " query, setting True for /chat/completions or False for /completions."
109
        )
110

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.