{"id":22539999,"url":"https://github.com/allanyiin/sqlrag","last_synced_at":"2025-04-09T21:43:47.450Z","repository":{"id":209253822,"uuid":"723586915","full_name":"AllanYiin/SQLRAG","owner":"AllanYiin","description":"SQL Server  as a vector database, SQL Server Extenstion for RAG","archived":false,"fork":false,"pushed_at":"2023-12-28T22:39:16.000Z","size":1207,"stargazers_count":24,"open_issues_count":0,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-23T23:35:16.984Z","etag":null,"topics":["rag","sql-server","vector-database"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/AllanYiin.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2023-11-26T06:07:00.000Z","updated_at":"2025-02-22T08:54:53.000Z","dependencies_parsed_at":"2023-11-26T07:24:53.916Z","dependency_job_id":"56a25738-0553-433d-aa31-a90039d9997f","html_url":"https://github.com/AllanYiin/SQLRAG","commit_stats":null,"previous_names":["allanyiin/sqlrag"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AllanYiin%2FSQLRAG","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AllanYiin%2FSQLRAG/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AllanYiin%2FSQLRAG/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AllanYiin%2FSQLRAG/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/AllanYiin","download_url":"https://codeload.github.com/AllanYiin/SQLRAG/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248119342,"owners_count":21050753,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["rag","sql-server","vector-database"],"created_at":"2024-12-07T12:09:20.286Z","updated_at":"2025-04-09T21:43:47.422Z","avatar_url":"https://github.com/AllanYiin.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# SQLRAG\n## 讓SQL Server也能執行向量相似性查詢\n\n目前已經實現功能:  \n- 各種相似性比較CLR函數: CosineSimilarity, EuclideanDistance...  [參考 (https://chat.openai.com/share/40723def-afdd-4f83-adb3-fbcae34b617a)]\n- 透過CLR函數調用Openai API(GetEmbedding, ChatCompletion...) \n\n\n## 安裝\n最小安裝要求:SQL Server 2022, Visual Studio 2022(需安裝SSDT)\n\n1. 首先要啟用SQL Server CLR:   \n```sql\n\tuse SQLRAG\n\tsp_configure 'show advanced options', 1;  \n\tRECONFIGURE;  \n\tsp_configure 'clr enabled', 1;  \n\tRECONFIGURE;  \n```\n\n2. 有以下幾種方式可以安裝:\n\t- 使用release中提供的SQLRAG_CREATE.sql直接執行即可完成安裝(適合首次安裝者)\n\t- 使用release中提供的dacpac檔，參考[網頁 (https://learn.microsoft.com/zh-tw/sql/relational-databases/data-tier-applications/upgrade-a-data-tier-application?view=sql-server-ver16)]內容安裝。\n\t- 使用release中提供的原始碼，使用Visual Studio 2022開啟後修改各專案的資料庫連線後直接部署方案\n\n3. 如果要使用OpenaiFunction，之前的做法是在OpenaiFunction.cs中輸入你實際的OPENAI_API_KEY，這樣的做法是不能確保API KEY的安全的。在這一版中，我在SqlRAG資料庫中增加了dbo.EncryptedKeys資料表，其中KeyValue部分透過憑證加密。看起來複雜但我已經將自動調用的部分處理好，開發者只需要透過以下SQL 語法將API KEY加密後寫入資料表中即可。  \n```sql\n\tuse SQLRAG\n\tDeclare @cleartext varchar(512)='sk-輸入你的OPENAI_API_KEYAPI KEY'\n\tDeclare @encrytext varbinary(4000)=EncryptByCert(Cert_ID('SqlRAGCertificate'), @cleartext) \n\n\tINSERT INTO [SQLRAG].[dbo].[EncryptedKeys] \n    \tVALUES ( N'OPENAI_API_KEY', N'調用OPENAI所用之API KEY',@encrytext );  \n```\n之後則需要透過顯式的聲明基於哪個憑證以及對應的密碼來進行解密:\n```sql\n\n    DecryptByCert(Cert_ID('SqlRAGCertificate'), EncryptByCert(Cert_ID('SqlRAGCertificate'), @cleartext) ,'P@ssw0rd')\n\n```\n\n如果你是Azure Openai Service用戶則需要將api key以及endpoint加密後存入\n```sql\n\n\tuse SQLRAG\n\tDeclare @cleartext varchar(512)='***'\n\tDeclare @encrytext varbinary(4000)=EncryptByCert(Cert_ID('SqlRAGCertificate'), @cleartext) \n\n\tDeclare @cleartext2 varchar(512)='https://***.openai.azure.com'\n\tDeclare @encrytext2 varbinary(4000)=EncryptByCert(Cert_ID('SqlRAGCertificate'), @cleartext2) \n\n\tDeclare @cleartext3 varchar(512)='***'\n\tDeclare @encrytext3 varbinary(4000)=EncryptByCert(Cert_ID('SqlRAGCertificate'), @cleartext3) \n\n\tINSERT INTO [SQLRAG].[dbo].[EncryptedKeys] \n    \tVALUES ( N'AZURE_OPENAI_API_KEY', N'調用Azure Openai Service 所用之API KEY',@encrytext );  \n\tINSERT INTO [SQLRAG].[dbo].[EncryptedKeys] \n    \tVALUES ( N'AZURE_OPENAI_ENDPOINT', N'調用Azure Openai Service 所用之endpoint',@encrytext2 );  \n\tINSERT INTO [SQLRAG].[dbo].[EncryptedKeys] \n    \tVALUES ( N'OPENAI_API_VERSION', N'調用Azure Openai Service 所用之API Version',@encrytext3 );  \n\n```\n\n\n\n4. assets中的QueryIntentCache_demo.sql (語意快取範例)以及ChatCompletion_demo.sql(ChatGPT回答範例)   \n\n![ChatCompletion](assets/QueryCache.png)\n![ChatCompletion](assets/ChatCompletion.png)\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fallanyiin%2Fsqlrag","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fallanyiin%2Fsqlrag","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fallanyiin%2Fsqlrag/lists"}