|
| 1 | +/*#info |
| 2 | + # Autor |
| 3 | + Rodrigo Ribeiro Gomes |
| 4 | +
|
| 5 | + # descricao |
| 6 | + Implementa a sp_AddEmbeddings para a Cohere |
| 7 | + Vide sp_AddEmbeddings.sql |
| 8 | +*/ |
| 9 | + |
| 10 | +CREATE OR ALTER PROC sp_GetEmbeddings_cohere ( |
| 11 | + @result nvarchar(max) OUTPUT |
| 12 | + ,@params json |
| 13 | +) |
| 14 | +AS |
| 15 | + |
| 16 | + DECLARE |
| 17 | + @texts nvarchar(max) = CONVERT(nvarchar(max),JSON_QUERY(@params,'$.texts')) |
| 18 | + ,@model varchar(max) = CONVERT(nvarchar(max),JSON_VALUE(@params,'$.model')) |
| 19 | + ,@dimensions int = CONVERT(int,JSON_VALUE(@params,'$.dimensions')) |
| 20 | + ,@options nvarchar(max) = CONVERT(nvarchar(max),JSON_QUERY(@params,'$.options')) |
| 21 | + ,@credential nvarchar(max) = CONVERT(nvarchar(max),JSON_VALUE(@params,'$.credential')) |
| 22 | + ,@IsExternal bit = CONVERT(bit,JSON_VALUE(@params,'$.IsExternal')) |
| 23 | + ,@url nvarchar(max) = CONVERT(nvarchar(max),JSON_VALUE(@params,'$.url')) -- url alternativa! |
| 24 | + |
| 25 | + |
| 26 | + |
| 27 | + declare @Debug bit = convert(int,SESSION_CONTEXT(N'AddEmbeddings-Debug')) |
| 28 | + |
| 29 | + |
| 30 | + if @model is null |
| 31 | + set @model = 'embed-v4.0' |
| 32 | + |
| 33 | + -- exige autenticacao! |
| 34 | + if @credential is null |
| 35 | + set @credential = 'https://api.cohere.com' |
| 36 | + |
| 37 | + -- https://docs.cohere.com/v2/reference/embed |
| 38 | + declare |
| 39 | + @SupportedModels TABLE(model varchar(200), Dimensions json) |
| 40 | + |
| 41 | + |
| 42 | + |
| 43 | + insert into @SupportedModels |
| 44 | + values |
| 45 | + ('embed-english-v3.0', '[1024]') |
| 46 | + ,('embed-multilingual-v3.0','[1024]') |
| 47 | + ,('embed-english-light-v3.0','[384]') |
| 48 | + ,('embed-multilingual-light-v3.0','[384]') |
| 49 | + ,('embed-english-v2.0','[4096]') |
| 50 | + ,('embed-english-light-v2.0','[1024]') |
| 51 | + ,('embed-multilingual-v2.0','[768]') |
| 52 | + ,('embed-v4.0','[256,512,1024,1536]') |
| 53 | + |
| 54 | + |
| 55 | + if not exists(select * From @SupportedModels where model = @model and JSON_CONTAINS(Dimensions,@dimensions,'$[*]') = 1) |
| 56 | + begin |
| 57 | + RAISERROR('Model %s not supported with dimensions %d',16,1,@model,@dimensions); |
| 58 | + return; |
| 59 | + end |
| 60 | + |
| 61 | + -- Agora é invocar! |
| 62 | + declare @response nvarchar(max) |
| 63 | + declare @retval int |
| 64 | + declare @reqoptions nvarchar(max) = ( |
| 65 | + select |
| 66 | + texts = JSON_QUERY(@texts) |
| 67 | + ,model = @model |
| 68 | + ,input_type = 'search_document' |
| 69 | + ,embedding_types = JSON_QUERY('["float"]') |
| 70 | + ,output_dimension = @dimensions |
| 71 | + for json path,without_array_wrapper |
| 72 | + ) |
| 73 | + |
| 74 | + if @Debug = 1 |
| 75 | + raiserror('[cohere] reqoptions: %s',0,1,@reqoptions) with nowait; |
| 76 | + |
| 77 | + exec @retval = sp_invoke_external_rest_endpoint |
| 78 | + @url = 'https://api.cohere.com/v2/embed', |
| 79 | + @method = 'POST', |
| 80 | + @credential = @credential, |
| 81 | + @payload = @reqoptions, |
| 82 | + @response = @response output |
| 83 | + |
| 84 | + if @Debug = 1 |
| 85 | + raiserror('[cohere] result: %s',0,1, @response ) with nowait; |
| 86 | + |
| 87 | + declare |
| 88 | + @HttpStatus int = JSON_VALUE(@response,'$.response.status.http.code') |
| 89 | + ,@HttpResult nvarchar(max) = JSON_QUERY(@response,'$.result') |
| 90 | + |
| 91 | + set @result = ( |
| 92 | + select |
| 93 | + ok = iif(@HttpStatus = 200,1,0) |
| 94 | + ,error = iif(@HttpStatus = 200,null,JSON_QUERY(@HttpResult)) |
| 95 | + ,embeddings = JSON_QUERY(@response,'$.result.embeddings.float') |
| 96 | + for json path, without_array_wrapper |
| 97 | + ) |
| 98 | + |
| 99 | +GO |
0 commit comments