|
| 1 | +import enum |
| 2 | +from typing import Optional, Dict, Any |
| 3 | + |
| 4 | +from ravendb.serverwide.server_operation_executor import ConnectionStringType |
| 5 | +from ravendb.documents.operations.ai.azure_open_ai_settings import AzureOpenAiSettings |
| 6 | +from ravendb.documents.operations.ai.embedded_settings import EmbeddedSettings |
| 7 | +from ravendb.documents.operations.ai.google_settings import GoogleSettings |
| 8 | +from ravendb.documents.operations.ai.hugging_face_settings import HuggingFaceSettings |
| 9 | +from ravendb.documents.operations.ai.mistral_ai_settings import MistralAiSettings |
| 10 | +from ravendb.documents.operations.ai.ollama_settings import OllamaSettings |
| 11 | +from ravendb.documents.operations.ai.open_ai_settings import OpenAiSettings |
| 12 | +from ravendb.documents.operations.connection_strings import ConnectionString |
| 13 | + |
| 14 | + |
| 15 | +class AiModelType(enum.Enum): |
| 16 | + TEXT_EMBEDDINGS = "TextEmbeddings" |
| 17 | + CHAT = "Chat" |
| 18 | + |
| 19 | + |
| 20 | +class AiConnectionString(ConnectionString): # todo kuba |
| 21 | + def __init__( |
| 22 | + self, |
| 23 | + name: str, |
| 24 | + identifier: str, |
| 25 | + openai_settings: Optional[OpenAiSettings] = None, |
| 26 | + azure_openai_settings: Optional[AzureOpenAiSettings] = None, |
| 27 | + ollama_settings: Optional[OllamaSettings] = None, |
| 28 | + embedded_settings: Optional[EmbeddedSettings] = None, |
| 29 | + google_settings: Optional[GoogleSettings] = None, |
| 30 | + huggingface_settings: Optional[HuggingFaceSettings] = None, |
| 31 | + mistral_ai_settings: Optional[MistralAiSettings] = None, |
| 32 | + model_type: AiModelType = None, |
| 33 | + ): |
| 34 | + super().__init__(name) |
| 35 | + self.identifier = identifier |
| 36 | + self.openai_settings = openai_settings |
| 37 | + self.azure_openai_settings = azure_openai_settings |
| 38 | + self.ollama_settings = ollama_settings |
| 39 | + self.embedded_settings = embedded_settings |
| 40 | + self.google_settings = google_settings |
| 41 | + self.huggingface_settings = huggingface_settings |
| 42 | + self.mistral_ai_settings = mistral_ai_settings |
| 43 | + self.model_type = model_type |
| 44 | + |
| 45 | + if not any( |
| 46 | + [ |
| 47 | + openai_settings, |
| 48 | + azure_openai_settings, |
| 49 | + ollama_settings, |
| 50 | + embedded_settings, |
| 51 | + google_settings, |
| 52 | + huggingface_settings, |
| 53 | + mistral_ai_settings, |
| 54 | + ] |
| 55 | + ): |
| 56 | + raise ValueError( |
| 57 | + "Please provide at least one of the following settings: openai_settings, azure_openai_settings, ollama_settings, embedded_settings, google_settings, huggingface_settings, mistral_ai_settings" |
| 58 | + ) |
| 59 | + |
| 60 | + if model_type is None: |
| 61 | + raise ValueError("Please provide a model type - AiModelType.TEXT_EMBEDDINGS or AiModelType.CHAT") |
| 62 | + |
| 63 | + settings_set_count = 0 |
| 64 | + for setting in [ |
| 65 | + openai_settings, |
| 66 | + azure_openai_settings, |
| 67 | + ollama_settings, |
| 68 | + embedded_settings, |
| 69 | + google_settings, |
| 70 | + huggingface_settings, |
| 71 | + mistral_ai_settings, |
| 72 | + ]: |
| 73 | + if setting: |
| 74 | + settings_set_count += 1 if setting else 0 |
| 75 | + if settings_set_count > 1: |
| 76 | + raise ValueError( |
| 77 | + "Please provide only one of the following settings: openai_settings, azure_openai_settings, ollama_settings, embedded_settings, google_settings, huggingface_settings, mistral_ai_settings" |
| 78 | + ) |
| 79 | + |
| 80 | + @property |
| 81 | + def get_type(self): |
| 82 | + return ConnectionStringType.AI.value |
| 83 | + |
| 84 | + def to_json(self) -> Dict[str, Any]: |
| 85 | + return { |
| 86 | + "Name": self.name, |
| 87 | + "Identifier": self.identifier, |
| 88 | + "OpenAiSettings": self.openai_settings.to_json() if self.openai_settings else None, |
| 89 | + "AzureOpenAiSettings": self.azure_openai_settings.to_json() if self.azure_openai_settings else None, |
| 90 | + "OllamaSettings": self.ollama_settings.to_json() if self.ollama_settings else None, |
| 91 | + "EmbeddedSettings": self.embedded_settings.to_json() if self.embedded_settings else None, |
| 92 | + "GoogleSettings": self.google_settings.to_json() if self.google_settings else None, |
| 93 | + "HuggingFaceSettings": self.huggingface_settings.to_json() if self.huggingface_settings else None, |
| 94 | + "MistralAiSettings": self.mistral_ai_settings.to_json() if self.mistral_ai_settings else None, |
| 95 | + "ModelType": self.model_type.value if self.model_type else None, |
| 96 | + "Type": self.get_type, |
| 97 | + } |
| 98 | + |
| 99 | + @classmethod |
| 100 | + def from_json(cls, json_dict: Dict[str, Any]) -> "AiConnectionString": |
| 101 | + return cls( |
| 102 | + name=json_dict["Name"], |
| 103 | + identifier=json_dict["Identifier"], |
| 104 | + openai_settings=( |
| 105 | + OpenAiSettings.from_json(json_dict["OpenAiSettings"]) if json_dict.get("OpenAiSettings") else None |
| 106 | + ), |
| 107 | + azure_openai_settings=( |
| 108 | + AzureOpenAiSettings.from_json(json_dict["AzureOpenAiSettings"]) |
| 109 | + if json_dict.get("AzureOpenAiSettings") |
| 110 | + else None |
| 111 | + ), |
| 112 | + ollama_settings=( |
| 113 | + OllamaSettings.from_json(json_dict["OllamaSettings"]) if json_dict.get("OllamaSettings") else None |
| 114 | + ), |
| 115 | + embedded_settings=( |
| 116 | + EmbeddedSettings.from_json(json_dict["EmbeddedSettings"]) if json_dict.get("EmbeddedSettings") else None |
| 117 | + ), |
| 118 | + google_settings=( |
| 119 | + GoogleSettings.from_json(json_dict["GoogleSettings"]) if json_dict.get("GoogleSettings") else None |
| 120 | + ), |
| 121 | + huggingface_settings=( |
| 122 | + HuggingFaceSettings.from_json(json_dict["HuggingFaceSettings"]) |
| 123 | + if json_dict.get("HuggingFaceSettings") |
| 124 | + else None |
| 125 | + ), |
| 126 | + mistral_ai_settings=( |
| 127 | + MistralAiSettings.from_json(json_dict["MistralAiSettings"]) |
| 128 | + if json_dict.get("MistralAiSettings") |
| 129 | + else None |
| 130 | + ), |
| 131 | + model_type=AiModelType(json_dict["ModelType"]) if json_dict.get("ModelType") else None, |
| 132 | + ) |
0 commit comments