Skip to content

Commit 672562a

Browse files
committed
refactor: use AI SDK cosineSimilarity instead of hand-rolled implementation
[skip northflank]
1 parent 86269b9 commit 672562a

4 files changed

Lines changed: 4 additions & 24 deletions

File tree

backend/src/lib/ai/rag-tools.ts

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { mkdir, readFile, readdir, writeFile } from 'node:fs/promises'
33
import { join, dirname } from 'node:path'
44
import { fileURLToPath } from 'node:url'
55
import { openai } from '@ai-sdk/openai'
6-
import { embed, embedMany } from 'ai'
6+
import { cosineSimilarity, embed, embedMany } from 'ai'
77
import { logger } from '../logger'
88

99
interface RagDocument {
@@ -68,27 +68,7 @@ function ensureAPIKey(): boolean {
6868
return true
6969
}
7070

71-
function cosineSimilarity(a: number[], b: number[]): number {
72-
if (a.length !== b.length || a.length === 0) {
73-
return 0
74-
}
75-
76-
let dot = 0
77-
let normA = 0
78-
let normB = 0
79-
80-
for (let i = 0; i < a.length; i++) {
81-
dot += a[i] * b[i]
82-
normA += a[i] * a[i]
83-
normB += b[i] * b[i]
84-
}
8571

86-
if (normA === 0 || normB === 0) {
87-
return 0
88-
}
89-
90-
return dot / (Math.sqrt(normA) * Math.sqrt(normB))
91-
}
9272

9373
async function ensureInitialized(): Promise<void> {
9474
if (knowledgeBase.length > 0) {

consent-app/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"displayName": "Proxy Smart Consent App",
44
"description": "SMART on FHIR consent management app — manage FHIR Consent resources for linked Patient records.",
55
"private": true,
6-
"version": "0.0.2-alpha.202603220311.23c239fe",
6+
"version": "0.0.2-alpha.202603220344.599fb64d",
77
"type": "module",
88
"scripts": {
99
"dev": "vite --port 5174",

dtr-app/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"displayName": "Proxy Smart DTR App",
44
"description": "Da Vinci DTR (Documentation Templates & Rules) — SMART on FHIR app for prior authorization documentation, questionnaire rendering, and CQL-based auto-population.",
55
"private": true,
6-
"version": "0.0.2-alpha.202603220311.23c239fe",
6+
"version": "0.0.2-alpha.202603220344.599fb64d",
77
"type": "module",
88
"scripts": {
99
"dev": "vite --port 5175",

shared-ui/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@proxy-smart/shared-ui",
3-
"version": "0.0.2-alpha.202603220311.23c239fe",
3+
"version": "0.0.2-alpha.202603220344.599fb64d",
44
"private": true,
55
"type": "module",
66
"main": "./src/index.ts",

0 commit comments

Comments
 (0)