snowflake-cli-labs 2.8.0rc1__py3-none-any.whl → 2.8.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (242) hide show
  1. README.md +21 -0
  2. {snowflake_cli_labs-2.8.0rc1.dist-info → snowflake_cli_labs-2.8.2.dist-info}/METADATA +7 -95
  3. snowflake_cli_labs-2.8.2.dist-info/RECORD +5 -0
  4. snowflake/cli/__about__.py +0 -17
  5. snowflake/cli/__init__.py +0 -13
  6. snowflake/cli/api/__init__.py +0 -48
  7. snowflake/cli/api/cli_global_context.py +0 -390
  8. snowflake/cli/api/commands/__init__.py +0 -13
  9. snowflake/cli/api/commands/alias.py +0 -23
  10. snowflake/cli/api/commands/decorators.py +0 -354
  11. snowflake/cli/api/commands/execution_metadata.py +0 -40
  12. snowflake/cli/api/commands/experimental_behaviour.py +0 -19
  13. snowflake/cli/api/commands/flags.py +0 -640
  14. snowflake/cli/api/commands/project_initialisation.py +0 -65
  15. snowflake/cli/api/commands/snow_typer.py +0 -237
  16. snowflake/cli/api/commands/typer_pre_execute.py +0 -26
  17. snowflake/cli/api/config.py +0 -348
  18. snowflake/cli/api/console/__init__.py +0 -17
  19. snowflake/cli/api/console/abc.py +0 -89
  20. snowflake/cli/api/console/console.py +0 -134
  21. snowflake/cli/api/console/enum.py +0 -17
  22. snowflake/cli/api/constants.py +0 -79
  23. snowflake/cli/api/errno.py +0 -27
  24. snowflake/cli/api/exceptions.py +0 -164
  25. snowflake/cli/api/feature_flags.py +0 -55
  26. snowflake/cli/api/identifiers.py +0 -154
  27. snowflake/cli/api/output/__init__.py +0 -13
  28. snowflake/cli/api/output/formats.py +0 -20
  29. snowflake/cli/api/output/types.py +0 -118
  30. snowflake/cli/api/plugins/__init__.py +0 -13
  31. snowflake/cli/api/plugins/command/__init__.py +0 -72
  32. snowflake/cli/api/plugins/command/plugin_hook_specs.py +0 -21
  33. snowflake/cli/api/plugins/plugin_config.py +0 -32
  34. snowflake/cli/api/project/__init__.py +0 -13
  35. snowflake/cli/api/project/definition.py +0 -84
  36. snowflake/cli/api/project/definition_manager.py +0 -134
  37. snowflake/cli/api/project/errors.py +0 -56
  38. snowflake/cli/api/project/project_verification.py +0 -23
  39. snowflake/cli/api/project/schemas/__init__.py +0 -13
  40. snowflake/cli/api/project/schemas/entities/application_entity.py +0 -44
  41. snowflake/cli/api/project/schemas/entities/application_package_entity.py +0 -66
  42. snowflake/cli/api/project/schemas/entities/common.py +0 -78
  43. snowflake/cli/api/project/schemas/entities/entities.py +0 -30
  44. snowflake/cli/api/project/schemas/identifier_model.py +0 -49
  45. snowflake/cli/api/project/schemas/native_app/__init__.py +0 -13
  46. snowflake/cli/api/project/schemas/native_app/application.py +0 -62
  47. snowflake/cli/api/project/schemas/native_app/native_app.py +0 -93
  48. snowflake/cli/api/project/schemas/native_app/package.py +0 -78
  49. snowflake/cli/api/project/schemas/native_app/path_mapping.py +0 -65
  50. snowflake/cli/api/project/schemas/project_definition.py +0 -199
  51. snowflake/cli/api/project/schemas/snowpark/__init__.py +0 -13
  52. snowflake/cli/api/project/schemas/snowpark/argument.py +0 -28
  53. snowflake/cli/api/project/schemas/snowpark/callable.py +0 -69
  54. snowflake/cli/api/project/schemas/snowpark/snowpark.py +0 -36
  55. snowflake/cli/api/project/schemas/streamlit/__init__.py +0 -13
  56. snowflake/cli/api/project/schemas/streamlit/streamlit.py +0 -46
  57. snowflake/cli/api/project/schemas/template.py +0 -77
  58. snowflake/cli/api/project/schemas/updatable_model.py +0 -194
  59. snowflake/cli/api/project/util.py +0 -261
  60. snowflake/cli/api/rendering/__init__.py +0 -13
  61. snowflake/cli/api/rendering/jinja.py +0 -112
  62. snowflake/cli/api/rendering/project_definition_templates.py +0 -39
  63. snowflake/cli/api/rendering/project_templates.py +0 -98
  64. snowflake/cli/api/rendering/sql_templates.py +0 -60
  65. snowflake/cli/api/rest_api.py +0 -172
  66. snowflake/cli/api/sanitizers.py +0 -43
  67. snowflake/cli/api/secure_path.py +0 -362
  68. snowflake/cli/api/secure_utils.py +0 -29
  69. snowflake/cli/api/sql_execution.py +0 -260
  70. snowflake/cli/api/utils/__init__.py +0 -13
  71. snowflake/cli/api/utils/cursor.py +0 -34
  72. snowflake/cli/api/utils/definition_rendering.py +0 -383
  73. snowflake/cli/api/utils/dict_utils.py +0 -73
  74. snowflake/cli/api/utils/error_handling.py +0 -23
  75. snowflake/cli/api/utils/graph.py +0 -97
  76. snowflake/cli/api/utils/models.py +0 -63
  77. snowflake/cli/api/utils/naming_utils.py +0 -13
  78. snowflake/cli/api/utils/path_utils.py +0 -36
  79. snowflake/cli/api/utils/templating_functions.py +0 -144
  80. snowflake/cli/api/utils/types.py +0 -35
  81. snowflake/cli/app/__init__.py +0 -22
  82. snowflake/cli/app/__main__.py +0 -31
  83. snowflake/cli/app/api_impl/__init__.py +0 -13
  84. snowflake/cli/app/api_impl/plugin/__init__.py +0 -13
  85. snowflake/cli/app/api_impl/plugin/plugin_config_provider_impl.py +0 -66
  86. snowflake/cli/app/build_and_push.sh +0 -8
  87. snowflake/cli/app/cli_app.py +0 -243
  88. snowflake/cli/app/commands_registration/__init__.py +0 -33
  89. snowflake/cli/app/commands_registration/builtin_plugins.py +0 -54
  90. snowflake/cli/app/commands_registration/command_plugins_loader.py +0 -169
  91. snowflake/cli/app/commands_registration/commands_registration_with_callbacks.py +0 -105
  92. snowflake/cli/app/commands_registration/exception_logging.py +0 -26
  93. snowflake/cli/app/commands_registration/threadsafe.py +0 -48
  94. snowflake/cli/app/commands_registration/typer_registration.py +0 -153
  95. snowflake/cli/app/constants.py +0 -19
  96. snowflake/cli/app/dev/__init__.py +0 -13
  97. snowflake/cli/app/dev/commands_structure.py +0 -48
  98. snowflake/cli/app/dev/docs/__init__.py +0 -13
  99. snowflake/cli/app/dev/docs/commands_docs_generator.py +0 -100
  100. snowflake/cli/app/dev/docs/generator.py +0 -35
  101. snowflake/cli/app/dev/docs/project_definition_docs_generator.py +0 -58
  102. snowflake/cli/app/dev/docs/project_definition_generate_json_schema.py +0 -227
  103. snowflake/cli/app/dev/docs/template_utils.py +0 -23
  104. snowflake/cli/app/dev/docs/templates/definition_description.rst.jinja2 +0 -38
  105. snowflake/cli/app/dev/docs/templates/overview.rst.jinja2 +0 -9
  106. snowflake/cli/app/dev/docs/templates/usage.rst.jinja2 +0 -57
  107. snowflake/cli/app/dev/pycharm_remote_debug.py +0 -46
  108. snowflake/cli/app/loggers.py +0 -199
  109. snowflake/cli/app/main_typer.py +0 -62
  110. snowflake/cli/app/printing.py +0 -181
  111. snowflake/cli/app/snow_connector.py +0 -243
  112. snowflake/cli/app/telemetry.py +0 -189
  113. snowflake/cli/plugins/__init__.py +0 -13
  114. snowflake/cli/plugins/connection/__init__.py +0 -13
  115. snowflake/cli/plugins/connection/commands.py +0 -330
  116. snowflake/cli/plugins/connection/plugin_spec.py +0 -30
  117. snowflake/cli/plugins/connection/util.py +0 -179
  118. snowflake/cli/plugins/cortex/__init__.py +0 -13
  119. snowflake/cli/plugins/cortex/commands.py +0 -327
  120. snowflake/cli/plugins/cortex/constants.py +0 -17
  121. snowflake/cli/plugins/cortex/manager.py +0 -189
  122. snowflake/cli/plugins/cortex/plugin_spec.py +0 -30
  123. snowflake/cli/plugins/cortex/types.py +0 -22
  124. snowflake/cli/plugins/git/__init__.py +0 -13
  125. snowflake/cli/plugins/git/commands.py +0 -305
  126. snowflake/cli/plugins/git/manager.py +0 -96
  127. snowflake/cli/plugins/git/plugin_spec.py +0 -30
  128. snowflake/cli/plugins/init/__init__.py +0 -13
  129. snowflake/cli/plugins/init/commands.py +0 -244
  130. snowflake/cli/plugins/init/plugin_spec.py +0 -30
  131. snowflake/cli/plugins/nativeapp/__init__.py +0 -13
  132. snowflake/cli/plugins/nativeapp/artifacts.py +0 -742
  133. snowflake/cli/plugins/nativeapp/codegen/__init__.py +0 -13
  134. snowflake/cli/plugins/nativeapp/codegen/artifact_processor.py +0 -91
  135. snowflake/cli/plugins/nativeapp/codegen/compiler.py +0 -130
  136. snowflake/cli/plugins/nativeapp/codegen/sandbox.py +0 -306
  137. snowflake/cli/plugins/nativeapp/codegen/setup/native_app_setup_processor.py +0 -172
  138. snowflake/cli/plugins/nativeapp/codegen/setup/setup_driver.py.source +0 -56
  139. snowflake/cli/plugins/nativeapp/codegen/snowpark/callback_source.py.jinja +0 -181
  140. snowflake/cli/plugins/nativeapp/codegen/snowpark/extension_function_utils.py +0 -217
  141. snowflake/cli/plugins/nativeapp/codegen/snowpark/models.py +0 -61
  142. snowflake/cli/plugins/nativeapp/codegen/snowpark/python_processor.py +0 -528
  143. snowflake/cli/plugins/nativeapp/commands.py +0 -439
  144. snowflake/cli/plugins/nativeapp/common_flags.py +0 -44
  145. snowflake/cli/plugins/nativeapp/constants.py +0 -27
  146. snowflake/cli/plugins/nativeapp/exceptions.py +0 -122
  147. snowflake/cli/plugins/nativeapp/feature_flags.py +0 -24
  148. snowflake/cli/plugins/nativeapp/init.py +0 -345
  149. snowflake/cli/plugins/nativeapp/manager.py +0 -823
  150. snowflake/cli/plugins/nativeapp/plugin_spec.py +0 -30
  151. snowflake/cli/plugins/nativeapp/policy.py +0 -50
  152. snowflake/cli/plugins/nativeapp/project_model.py +0 -195
  153. snowflake/cli/plugins/nativeapp/run_processor.py +0 -389
  154. snowflake/cli/plugins/nativeapp/teardown_processor.py +0 -301
  155. snowflake/cli/plugins/nativeapp/utils.py +0 -98
  156. snowflake/cli/plugins/nativeapp/v2_conversions/v2_to_v1_decorator.py +0 -135
  157. snowflake/cli/plugins/nativeapp/version/__init__.py +0 -13
  158. snowflake/cli/plugins/nativeapp/version/commands.py +0 -170
  159. snowflake/cli/plugins/nativeapp/version/version_processor.py +0 -362
  160. snowflake/cli/plugins/notebook/__init__.py +0 -13
  161. snowflake/cli/plugins/notebook/commands.py +0 -84
  162. snowflake/cli/plugins/notebook/exceptions.py +0 -20
  163. snowflake/cli/plugins/notebook/manager.py +0 -71
  164. snowflake/cli/plugins/notebook/plugin_spec.py +0 -30
  165. snowflake/cli/plugins/notebook/types.py +0 -16
  166. snowflake/cli/plugins/object/__init__.py +0 -13
  167. snowflake/cli/plugins/object/command_aliases.py +0 -94
  168. snowflake/cli/plugins/object/commands.py +0 -174
  169. snowflake/cli/plugins/object/common.py +0 -85
  170. snowflake/cli/plugins/object/manager.py +0 -96
  171. snowflake/cli/plugins/object/plugin_spec.py +0 -30
  172. snowflake/cli/plugins/object_stage_deprecated/__init__.py +0 -15
  173. snowflake/cli/plugins/object_stage_deprecated/commands.py +0 -122
  174. snowflake/cli/plugins/object_stage_deprecated/plugin_spec.py +0 -32
  175. snowflake/cli/plugins/snowpark/__init__.py +0 -13
  176. snowflake/cli/plugins/snowpark/commands.py +0 -548
  177. snowflake/cli/plugins/snowpark/common.py +0 -307
  178. snowflake/cli/plugins/snowpark/manager.py +0 -109
  179. snowflake/cli/plugins/snowpark/models.py +0 -156
  180. snowflake/cli/plugins/snowpark/package/__init__.py +0 -13
  181. snowflake/cli/plugins/snowpark/package/anaconda_packages.py +0 -233
  182. snowflake/cli/plugins/snowpark/package/commands.py +0 -256
  183. snowflake/cli/plugins/snowpark/package/manager.py +0 -43
  184. snowflake/cli/plugins/snowpark/package/utils.py +0 -26
  185. snowflake/cli/plugins/snowpark/package_utils.py +0 -354
  186. snowflake/cli/plugins/snowpark/plugin_spec.py +0 -30
  187. snowflake/cli/plugins/snowpark/snowpark_package_paths.py +0 -65
  188. snowflake/cli/plugins/snowpark/snowpark_shared.py +0 -95
  189. snowflake/cli/plugins/snowpark/zipper.py +0 -81
  190. snowflake/cli/plugins/spcs/__init__.py +0 -35
  191. snowflake/cli/plugins/spcs/common.py +0 -99
  192. snowflake/cli/plugins/spcs/compute_pool/__init__.py +0 -13
  193. snowflake/cli/plugins/spcs/compute_pool/commands.py +0 -240
  194. snowflake/cli/plugins/spcs/compute_pool/manager.py +0 -121
  195. snowflake/cli/plugins/spcs/image_registry/__init__.py +0 -13
  196. snowflake/cli/plugins/spcs/image_registry/commands.py +0 -65
  197. snowflake/cli/plugins/spcs/image_registry/manager.py +0 -105
  198. snowflake/cli/plugins/spcs/image_repository/__init__.py +0 -13
  199. snowflake/cli/plugins/spcs/image_repository/commands.py +0 -196
  200. snowflake/cli/plugins/spcs/image_repository/manager.py +0 -84
  201. snowflake/cli/plugins/spcs/jobs/__init__.py +0 -13
  202. snowflake/cli/plugins/spcs/jobs/commands.py +0 -78
  203. snowflake/cli/plugins/spcs/jobs/manager.py +0 -53
  204. snowflake/cli/plugins/spcs/plugin_spec.py +0 -30
  205. snowflake/cli/plugins/spcs/services/__init__.py +0 -13
  206. snowflake/cli/plugins/spcs/services/commands.py +0 -311
  207. snowflake/cli/plugins/spcs/services/manager.py +0 -170
  208. snowflake/cli/plugins/sql/__init__.py +0 -13
  209. snowflake/cli/plugins/sql/commands.py +0 -83
  210. snowflake/cli/plugins/sql/manager.py +0 -92
  211. snowflake/cli/plugins/sql/plugin_spec.py +0 -30
  212. snowflake/cli/plugins/sql/snowsql_templating.py +0 -28
  213. snowflake/cli/plugins/stage/__init__.py +0 -13
  214. snowflake/cli/plugins/stage/commands.py +0 -261
  215. snowflake/cli/plugins/stage/diff.py +0 -326
  216. snowflake/cli/plugins/stage/manager.py +0 -544
  217. snowflake/cli/plugins/stage/md5.py +0 -160
  218. snowflake/cli/plugins/stage/plugin_spec.py +0 -30
  219. snowflake/cli/plugins/streamlit/__init__.py +0 -13
  220. snowflake/cli/plugins/streamlit/commands.py +0 -186
  221. snowflake/cli/plugins/streamlit/manager.py +0 -222
  222. snowflake/cli/plugins/streamlit/plugin_spec.py +0 -30
  223. snowflake/cli/plugins/workspace/__init__.py +0 -13
  224. snowflake/cli/plugins/workspace/commands.py +0 -35
  225. snowflake/cli/plugins/workspace/plugin_spec.py +0 -30
  226. snowflake/cli/templates/default_snowpark/.gitignore +0 -4
  227. snowflake/cli/templates/default_snowpark/app/__init__.py +0 -0
  228. snowflake/cli/templates/default_snowpark/app/common.py +0 -2
  229. snowflake/cli/templates/default_snowpark/app/functions.py +0 -15
  230. snowflake/cli/templates/default_snowpark/app/procedures.py +0 -22
  231. snowflake/cli/templates/default_snowpark/requirements.txt +0 -1
  232. snowflake/cli/templates/default_snowpark/snowflake.yml +0 -23
  233. snowflake/cli/templates/default_streamlit/.gitignore +0 -4
  234. snowflake/cli/templates/default_streamlit/common/hello.py +0 -2
  235. snowflake/cli/templates/default_streamlit/environment.yml +0 -6
  236. snowflake/cli/templates/default_streamlit/pages/my_page.py +0 -3
  237. snowflake/cli/templates/default_streamlit/snowflake.yml +0 -10
  238. snowflake/cli/templates/default_streamlit/streamlit_app.py +0 -4
  239. snowflake_cli_labs-2.8.0rc1.dist-info/RECORD +0 -240
  240. snowflake_cli_labs-2.8.0rc1.dist-info/entry_points.txt +0 -2
  241. {snowflake_cli_labs-2.8.0rc1.dist-info → snowflake_cli_labs-2.8.2.dist-info}/WHEEL +0 -0
  242. {snowflake_cli_labs-2.8.0rc1.dist-info → snowflake_cli_labs-2.8.2.dist-info}/licenses/LICENSE +0 -0
@@ -1,327 +0,0 @@
1
- # Copyright (c) 2024 Snowflake Inc.
2
- #
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at
6
- #
7
- # http://www.apache.org/licenses/LICENSE-2.0
8
- #
9
- # Unless required by applicable law or agreed to in writing, software
10
- # distributed under the License is distributed on an "AS IS" BASIS,
11
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- # See the License for the specific language governing permissions and
13
- # limitations under the License.
14
-
15
- from __future__ import annotations
16
-
17
- import sys
18
- from pathlib import Path
19
- from typing import List, Optional
20
-
21
- import click
22
- import typer
23
- from click import UsageError
24
- from snowflake.cli.api.cli_global_context import cli_context
25
- from snowflake.cli.api.commands.flags import readable_file_option
26
- from snowflake.cli.api.commands.snow_typer import SnowTyperFactory
27
- from snowflake.cli.api.constants import PYTHON_3_12
28
- from snowflake.cli.api.output.types import (
29
- CollectionResult,
30
- CommandResult,
31
- MessageResult,
32
- )
33
- from snowflake.cli.api.secure_path import SecurePath
34
- from snowflake.cli.plugins.cortex.constants import DEFAULT_MODEL
35
- from snowflake.cli.plugins.cortex.manager import CortexManager
36
- from snowflake.cli.plugins.cortex.types import (
37
- Language,
38
- Model,
39
- Question,
40
- SourceDocument,
41
- Text,
42
- )
43
-
44
- app = SnowTyperFactory(
45
- name="cortex",
46
- help="Provides access to Snowflake Cortex.",
47
- )
48
-
49
- SEARCH_COMMAND_ENABLED = sys.version_info < PYTHON_3_12
50
-
51
-
52
- @app.command(
53
- requires_connection=True,
54
- hidden=not SEARCH_COMMAND_ENABLED,
55
- )
56
- def search(
57
- query: str = typer.Argument(help="The search query string"),
58
- service: str = typer.Option(
59
- help="Cortex search service to be used. Example: --service my_cortex_service",
60
- ),
61
- columns: Optional[List[str]] = typer.Option(
62
- help='Columns that will be returned with the results. If none is provided, only search column will be included in results. Example --columns "foo" --columns "bar"',
63
- default=None,
64
- ),
65
- limit: int = typer.Option(help="Maximum number of results retrieved", default=1),
66
- **options,
67
- ):
68
- """
69
- Performs query search using Cortex Search Services.
70
- """
71
-
72
- if not SEARCH_COMMAND_ENABLED:
73
- raise click.ClickException(
74
- "Cortex Search uses Snowflake Python API that currently does not support your Python version"
75
- )
76
-
77
- from snowflake.core import Root
78
-
79
- if not columns:
80
- columns = []
81
-
82
- conn = cli_context.connection
83
-
84
- search_service = (
85
- Root(conn)
86
- .databases[conn.database]
87
- .schemas[conn.schema]
88
- .cortex_search_services[service]
89
- )
90
-
91
- response = search_service.search(
92
- query=query, columns=columns, limit=limit, filter={}
93
- )
94
-
95
- return CollectionResult(response.results)
96
-
97
-
98
- @app.command(
99
- name="complete",
100
- requires_connection=True,
101
- )
102
- def complete(
103
- text: Optional[str] = typer.Argument(
104
- None,
105
- help="Prompt to be used to generate a completion. Cannot be combined with --file option.",
106
- show_default=False,
107
- ),
108
- model: Optional[str] = typer.Option(
109
- DEFAULT_MODEL,
110
- "--model",
111
- help="String specifying the model to be used.",
112
- ),
113
- file: Optional[Path] = readable_file_option(
114
- param_name="--file",
115
- help_str="JSON file containing conversation history to be used to generate a completion. Cannot be combined with TEXT argument.",
116
- ),
117
- **options,
118
- ) -> CommandResult:
119
- """
120
- Given a prompt, the command generates a response using your choice of language model.
121
- In the simplest use case, the prompt is a single string.
122
- You may also provide a JSON file with conversation history including multiple prompts and responses for interactive chat-style usage.
123
- """
124
-
125
- manager = CortexManager()
126
-
127
- if text and file:
128
- raise UsageError("--file option cannot be used together with TEXT argument.")
129
- if text:
130
- result_text = manager.complete_for_prompt(
131
- text=Text(text),
132
- model=Model(model),
133
- )
134
- elif file:
135
- result_text = manager.complete_for_conversation(
136
- conversation_json_file=SecurePath(file),
137
- model=Model(model),
138
- )
139
- else:
140
- raise UsageError("Either --file option or TEXT argument has to be provided.")
141
-
142
- return MessageResult(result_text.strip())
143
-
144
-
145
- @app.command(
146
- name="extract-answer",
147
- requires_connection=True,
148
- )
149
- def extract_answer(
150
- question: str = typer.Argument(
151
- None,
152
- help="String containing the question to be answered.",
153
- show_default=False,
154
- ),
155
- source_document_text: Optional[str] = typer.Argument(
156
- None,
157
- help="String containing the plain-text or JSON document that contains the answer to the question. Cannot be combined with --file option.",
158
- show_default=False,
159
- ),
160
- file: Optional[Path] = readable_file_option(
161
- param_name="--file",
162
- help_str="File containing the plain-text or JSON document that contains the answer to the question. Cannot be combined with SOURCE_DOCUMENT_TEXT argument.",
163
- ),
164
- **options,
165
- ) -> CommandResult:
166
- """
167
- Extracts an answer to a given question from a text document.
168
- The document may be a plain-English document or a string representation of a semi-structured (JSON) data object.
169
- """
170
-
171
- manager = CortexManager()
172
-
173
- if source_document_text and file:
174
- raise UsageError(
175
- "--file option cannot be used together with SOURCE_DOCUMENT_TEXT argument."
176
- )
177
- if source_document_text:
178
- result_text = manager.extract_answer_from_source_document(
179
- source_document=SourceDocument(source_document_text),
180
- question=Question(question),
181
- )
182
- elif file:
183
- result_text = manager.extract_answer_from_source_document_file(
184
- source_document_input_file=SecurePath(file),
185
- question=Question(question),
186
- )
187
- else:
188
- raise UsageError(
189
- "Either --file option or SOURCE_DOCUMENT_TEXT argument has to be provided."
190
- )
191
-
192
- return MessageResult(result_text.strip())
193
-
194
-
195
- @app.command(
196
- name="sentiment",
197
- requires_connection=True,
198
- )
199
- def sentiment(
200
- text: Optional[str] = typer.Argument(
201
- None,
202
- help="String containing the text for which a sentiment score should be calculated. Cannot be combined with --file option.",
203
- show_default=False,
204
- ),
205
- file: Optional[Path] = readable_file_option(
206
- param_name="--file",
207
- help_str="File containing the text for which a sentiment score should be calculated. Cannot be combined with TEXT argument.",
208
- ),
209
- **options,
210
- ) -> CommandResult:
211
- """
212
- Returns sentiment as a score between -1 to 1
213
- (with -1 being the most negative and 1 the most positive,
214
- with values around 0 neutral) for the given English-language input text.
215
- """
216
-
217
- manager = CortexManager()
218
-
219
- if text and file:
220
- raise UsageError("--file option cannot be used together with TEXT argument.")
221
- if text:
222
- result_text = manager.calculate_sentiment_for_text(
223
- text=Text(text),
224
- )
225
- elif file:
226
- result_text = manager.calculate_sentiment_for_text_file(
227
- text_file=SecurePath(file),
228
- )
229
- else:
230
- raise UsageError("Either --file option or TEXT argument has to be provided.")
231
-
232
- return MessageResult(result_text.strip())
233
-
234
-
235
- @app.command(
236
- name="summarize",
237
- requires_connection=True,
238
- )
239
- def summarize(
240
- text: Optional[str] = typer.Argument(
241
- None,
242
- help="String containing the English text from which a summary should be generated. Cannot be combined with --file option.",
243
- show_default=False,
244
- ),
245
- file: Optional[Path] = readable_file_option(
246
- param_name="--file",
247
- help_str="File containing the English text from which a summary should be generated. Cannot be combined with TEXT argument.",
248
- ),
249
- **options,
250
- ) -> CommandResult:
251
- """
252
- Summarizes the given English-language input text.
253
- """
254
-
255
- manager = CortexManager()
256
-
257
- if text and file:
258
- raise UsageError("--file option cannot be used together with TEXT argument.")
259
- if text:
260
- result_text = manager.summarize_text(
261
- text=Text(text),
262
- )
263
- elif file:
264
- result_text = manager.summarize_text_file(
265
- text_file=SecurePath(file),
266
- )
267
- else:
268
- raise UsageError("Either --file option or TEXT argument has to be provided.")
269
-
270
- return MessageResult(result_text.strip())
271
-
272
-
273
- @app.command(
274
- name="translate",
275
- requires_connection=True,
276
- )
277
- def translate(
278
- text: Optional[str] = typer.Argument(
279
- None,
280
- help="String containing the text to be translated. Cannot be combined with --file option.",
281
- show_default=False,
282
- ),
283
- from_language: Optional[str] = typer.Option(
284
- None,
285
- "--from",
286
- help="String specifying the language code for the language the text is currently in. See Snowflake Cortex documentation for a list of supported language codes.",
287
- show_default=False,
288
- ),
289
- to_language: str = typer.Option(
290
- ...,
291
- "--to",
292
- help="String specifying the language code into which the text should be translated. See Snowflake Cortex documentation for a list of supported language codes.",
293
- show_default=False,
294
- ),
295
- file: Optional[Path] = readable_file_option(
296
- param_name="--file",
297
- help_str="File containing the text to be translated. Cannot be combined with TEXT argument.",
298
- ),
299
- **options,
300
- ) -> CommandResult:
301
- """
302
- Translates text from the indicated or detected source language to a target language.
303
- """
304
-
305
- manager = CortexManager()
306
-
307
- source_language = None if from_language is None else Language(from_language)
308
- target_language = Language(to_language)
309
-
310
- if text and file:
311
- raise UsageError("--file option cannot be used together with TEXT argument.")
312
- if text:
313
- result_text = manager.translate_text(
314
- text=Text(text),
315
- source_language=source_language,
316
- target_language=target_language,
317
- )
318
- elif file:
319
- result_text = manager.translate_text_file(
320
- text_file=SecurePath(file),
321
- source_language=source_language,
322
- target_language=target_language,
323
- )
324
- else:
325
- raise UsageError("Either --file option or TEXT argument has to be provided.")
326
-
327
- return MessageResult(result_text.strip())
@@ -1,17 +0,0 @@
1
- # Copyright (c) 2024 Snowflake Inc.
2
- #
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at
6
- #
7
- # http://www.apache.org/licenses/LICENSE-2.0
8
- #
9
- # Unless required by applicable law or agreed to in writing, software
10
- # distributed under the License is distributed on an "AS IS" BASIS,
11
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- # See the License for the specific language governing permissions and
13
- # limitations under the License.
14
-
15
- from snowflake.cli.plugins.cortex.types import Model
16
-
17
- DEFAULT_MODEL: Model = Model("snowflake-arctic")
@@ -1,189 +0,0 @@
1
- # Copyright (c) 2024 Snowflake Inc.
2
- #
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at
6
- #
7
- # http://www.apache.org/licenses/LICENSE-2.0
8
- #
9
- # Unless required by applicable law or agreed to in writing, software
10
- # distributed under the License is distributed on an "AS IS" BASIS,
11
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- # See the License for the specific language governing permissions and
13
- # limitations under the License.
14
-
15
- from __future__ import annotations
16
-
17
- import json
18
- import logging
19
- from typing import Callable, Optional
20
-
21
- from click import ClickException
22
- from snowflake.cli.api.constants import DEFAULT_SIZE_LIMIT_MB
23
- from snowflake.cli.api.exceptions import SnowflakeSQLExecutionError
24
- from snowflake.cli.api.secure_path import SecurePath
25
- from snowflake.cli.api.sql_execution import SqlExecutionMixin
26
- from snowflake.cli.plugins.cortex.types import (
27
- Language,
28
- Model,
29
- Question,
30
- SourceDocument,
31
- Text,
32
- )
33
- from snowflake.connector import ProgrammingError
34
- from snowflake.connector.cursor import DictCursor
35
-
36
- log = logging.getLogger(__name__)
37
-
38
-
39
- class CortexManager(SqlExecutionMixin):
40
- def complete_for_prompt(
41
- self,
42
- text: Text,
43
- model: Model,
44
- ) -> str:
45
- query = f"""\
46
- SELECT SNOWFLAKE.CORTEX.COMPLETE(
47
- '{model}',
48
- '{self._escape_input(text)}'
49
- ) AS CORTEX_RESULT;"""
50
- return self._query_cortex_result_str(query)
51
-
52
- def complete_for_conversation(
53
- self,
54
- conversation_json_file: SecurePath,
55
- model: Model,
56
- ) -> str:
57
- json_content = conversation_json_file.read_text(
58
- file_size_limit_mb=DEFAULT_SIZE_LIMIT_MB
59
- )
60
- query = f"""\
61
- SELECT SNOWFLAKE.CORTEX.COMPLETE(
62
- '{model}',
63
- PARSE_JSON('{self._escape_input(json_content)}'),
64
- {{}}
65
- ) AS CORTEX_RESULT;"""
66
- raw_result = self._query_cortex_result_str(query)
67
- json_result = json.loads(raw_result)
68
- return self._extract_text_result_from_json_result(
69
- lambda: json_result["choices"][0]["messages"]
70
- )
71
-
72
- def extract_answer_from_source_document(
73
- self,
74
- source_document: SourceDocument,
75
- question: Question,
76
- ) -> str:
77
- query = f"""\
78
- SELECT SNOWFLAKE.CORTEX.EXTRACT_ANSWER(
79
- '{self._escape_input(source_document)}',
80
- '{self._escape_input(question)}'
81
- ) AS CORTEX_RESULT;"""
82
- raw_result = self._query_cortex_result_str(query)
83
- json_result = json.loads(raw_result)
84
- return self._extract_text_result_from_json_result(
85
- lambda: json_result[0]["answer"]
86
- )
87
-
88
- def extract_answer_from_source_document_file(
89
- self,
90
- source_document_input_file: SecurePath,
91
- question: Question,
92
- ) -> str:
93
- source_document_content = source_document_input_file.read_text(
94
- file_size_limit_mb=DEFAULT_SIZE_LIMIT_MB
95
- )
96
- return self.extract_answer_from_source_document(
97
- source_document=SourceDocument(source_document_content), question=question
98
- )
99
-
100
- def calculate_sentiment_for_text(
101
- self,
102
- text: Text,
103
- ) -> str:
104
- query = f"""\
105
- SELECT SNOWFLAKE.CORTEX.SENTIMENT(
106
- '{self._escape_input(text)}'
107
- ) AS CORTEX_RESULT;"""
108
- return self._query_cortex_result_str(query)
109
-
110
- def calculate_sentiment_for_text_file(
111
- self,
112
- text_file: SecurePath,
113
- ) -> str:
114
- file_content = text_file.read_text(file_size_limit_mb=DEFAULT_SIZE_LIMIT_MB)
115
- return self.calculate_sentiment_for_text(
116
- text=Text(file_content),
117
- )
118
-
119
- def summarize_text(
120
- self,
121
- text: Text,
122
- ) -> str:
123
- query = f"""\
124
- SELECT SNOWFLAKE.CORTEX.SUMMARIZE(
125
- '{self._escape_input(text)}'
126
- ) AS CORTEX_RESULT;"""
127
- return self._query_cortex_result_str(query)
128
-
129
- def summarize_text_file(
130
- self,
131
- text_file: SecurePath,
132
- ) -> str:
133
- file_content = text_file.read_text(file_size_limit_mb=DEFAULT_SIZE_LIMIT_MB)
134
- return self.summarize_text(
135
- text=Text(file_content),
136
- )
137
-
138
- def translate_text(
139
- self,
140
- text: Text,
141
- source_language: Optional[Language],
142
- target_language: Language,
143
- ) -> str:
144
- query = f"""\
145
- SELECT SNOWFLAKE.CORTEX.TRANSLATE(
146
- '{self._escape_input(text)}',
147
- '{source_language or ""}',
148
- '{target_language}'
149
- ) AS CORTEX_RESULT;"""
150
- return self._query_cortex_result_str(query)
151
-
152
- def translate_text_file(
153
- self,
154
- text_file: SecurePath,
155
- source_language: Optional[Language],
156
- target_language: Language,
157
- ) -> str:
158
- file_content = text_file.read_text(file_size_limit_mb=DEFAULT_SIZE_LIMIT_MB)
159
- return self.translate_text(
160
- text=Text(file_content),
161
- source_language=source_language,
162
- target_language=target_language,
163
- )
164
-
165
- @staticmethod
166
- def _escape_input(plain_input: str):
167
- # escape backslashes to not escape too much, this replace has to be the first one
168
- # escape single quotes because they are wrapping the whole string in SQL
169
- return plain_input.replace("\\", "\\\\").replace("'", "\\'")
170
-
171
- @staticmethod
172
- def _extract_text_result_from_json_result(
173
- extract_function: Callable[[], str]
174
- ) -> str:
175
- try:
176
- return extract_function()
177
- except (KeyError, IndexError) as ex:
178
- log.debug("Cannot find Cortex result message in a response", exc_info=ex)
179
- raise ClickException("Unexpected format of response from Snowflake")
180
-
181
- def _query_cortex_result_str(self, query: str) -> str:
182
- try:
183
- cursor = self._execute_query(query, cursor_class=DictCursor)
184
- if cursor.rowcount is None:
185
- raise SnowflakeSQLExecutionError(query)
186
- return str(cursor.fetchone()["CORTEX_RESULT"])
187
- except ProgrammingError as ex:
188
- log.debug("ProgrammingError occurred during SQL execution", exc_info=ex)
189
- raise ClickException(str(ex))
@@ -1,30 +0,0 @@
1
- # Copyright (c) 2024 Snowflake Inc.
2
- #
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at
6
- #
7
- # http://www.apache.org/licenses/LICENSE-2.0
8
- #
9
- # Unless required by applicable law or agreed to in writing, software
10
- # distributed under the License is distributed on an "AS IS" BASIS,
11
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- # See the License for the specific language governing permissions and
13
- # limitations under the License.
14
-
15
- from snowflake.cli.api.plugins.command import (
16
- SNOWCLI_ROOT_COMMAND_PATH,
17
- CommandSpec,
18
- CommandType,
19
- plugin_hook_impl,
20
- )
21
- from snowflake.cli.plugins.cortex import commands
22
-
23
-
24
- @plugin_hook_impl
25
- def command_spec():
26
- return CommandSpec(
27
- parent_command_path=SNOWCLI_ROOT_COMMAND_PATH,
28
- command_type=CommandType.COMMAND_GROUP,
29
- typer_instance=commands.app.create_instance(),
30
- )
@@ -1,22 +0,0 @@
1
- # Copyright (c) 2024 Snowflake Inc.
2
- #
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at
6
- #
7
- # http://www.apache.org/licenses/LICENSE-2.0
8
- #
9
- # Unless required by applicable law or agreed to in writing, software
10
- # distributed under the License is distributed on an "AS IS" BASIS,
11
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- # See the License for the specific language governing permissions and
13
- # limitations under the License.
14
-
15
- from typing import NewType
16
-
17
- Text = NewType("Text", str)
18
- Model = NewType("Model", str)
19
- Language = NewType("Language", str)
20
- InputString = NewType("InputString", str)
21
- Question = NewType("Question", str)
22
- SourceDocument = NewType("SourceDocument", str)
@@ -1,13 +0,0 @@
1
- # Copyright (c) 2024 Snowflake Inc.
2
- #
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at
6
- #
7
- # http://www.apache.org/licenses/LICENSE-2.0
8
- #
9
- # Unless required by applicable law or agreed to in writing, software
10
- # distributed under the License is distributed on an "AS IS" BASIS,
11
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- # See the License for the specific language governing permissions and
13
- # limitations under the License.