|
1 | 1 | import enum |
2 | 2 | from typing import Optional, Dict, Any |
3 | 3 |
|
4 | | -import ravendb.serverwide.server_operation_executor |
| 4 | +from ravendb.serverwide.server_operation_executor import ConnectionStringType |
5 | 5 | from ravendb.documents.operations.ai.azure_open_ai_settings import AzureOpenAiSettings |
6 | 6 | from ravendb.documents.operations.ai.embedded_settings import EmbeddedSettings |
7 | 7 | from ravendb.documents.operations.ai.google_settings import GoogleSettings |
|
13 | 13 |
|
14 | 14 |
|
15 | 15 | class AiModelType(enum.Enum): |
16 | | - TEXT_EMBEDDINGS = ("TextEmbeddings",) |
| 16 | + TEXT_EMBEDDINGS = "TextEmbeddings" |
17 | 17 | CHAT = "Chat" |
18 | 18 |
|
19 | 19 |
|
@@ -42,36 +42,91 @@ def __init__( |
42 | 42 | self.mistral_ai_settings = mistral_ai_settings |
43 | 43 | self.model_type = model_type |
44 | 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 | + |
45 | 80 | @property |
46 | 81 | def get_type(self): |
47 | | - return ravendb.serverwide.server_operation_executor.ConnectionStringType.AI.value |
| 82 | + return ConnectionStringType.AI.value |
48 | 83 |
|
49 | 84 | def to_json(self) -> Dict[str, Any]: |
50 | 85 | return { |
51 | 86 | "Name": self.name, |
52 | 87 | "Identifier": self.identifier, |
53 | | - "OpenaiSettings": self.openai_settings, |
54 | | - "AzureOpenaiSettings": self.azure_openai_settings, |
55 | | - "ollama_settings": self.ollama_settings, |
56 | | - "EmbeddedSettings": self.embedded_settings, |
57 | | - "GoogleSettings": self.google_settings, |
58 | | - "HuggingfaceSettings": self.huggingface_settings, |
59 | | - "MistralAiSettings": self.mistral_ai_settings, |
60 | | - "ModelType": self.model_type, |
61 | | - "Type": ravendb.serverwide.server_operation_executor.ConnectionStringType.OLAP, |
| 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, |
62 | 97 | } |
63 | 98 |
|
64 | 99 | @classmethod |
65 | 100 | def from_json(cls, json_dict: Dict[str, Any]) -> "AiConnectionString": |
66 | 101 | return cls( |
67 | 102 | name=json_dict["Name"], |
68 | 103 | identifier=json_dict["Identifier"], |
69 | | - openai_settings=OpenAiSettings.from_json(json_dict["OpenaiSettings"]), |
70 | | - azure_openai_settings=AzureOpenAiSettings.from_json(json_dict["AzureOpenaiSettings"]), |
71 | | - ollama_settings=OllamaSettings.from_json(json_dict["ollama_settings"]), |
72 | | - embedded_settings=EmbeddedSettings.from_json(json_dict["EmbeddedSettings"]), |
73 | | - google_settings=GoogleSettings.from_json(json_dict["GoogleSettings"]), |
74 | | - huggingface_settings=HuggingFaceSettings.from_json(json_dict["HuggingfaceSettings"]), |
75 | | - mistral_ai_settings=MistralAiSettings.from_json(json_dict["MistralAiSettings"]), |
76 | | - model_type=AiModelType(json_dict["ModelType"]) if json_dict["ModelType"] else None, |
| 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, |
77 | 132 | ) |
0 commit comments