-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
517 additions
and
468 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,26 @@ | ||
{ | ||
"name": "@digdir/assistant-lib", | ||
"private": true, | ||
"version": "1.0.0", | ||
"description": "Core assistant library", | ||
"main": "dist/src/index.js", | ||
"license": "MIT", | ||
"dependencies": { | ||
"@azure/openai": "^1.0.0-beta.11", | ||
"@instructor-ai/instructor": "^0.0.7", | ||
"@supabase/supabase-js": "^2.39.7", | ||
"axios": "^1.6.7", | ||
"dataclass": "^2.1.1", | ||
"js-yaml": "^4.1.0", | ||
"lodash": "^4.17.21", | ||
"openai": "^4.28.0", | ||
"remeda": "^1.44.1", | ||
"typesense": "^1.7.2", | ||
"zod": "^3.22.4", | ||
"zod-to-json-schema": "^3.22.4" | ||
} | ||
"name": "@digdir/assistant-lib", | ||
"private": true, | ||
"version": "1.0.0", | ||
"description": "Core assistant library", | ||
"main": "dist/src/index.js", | ||
"license": "MIT", | ||
"scripts": { | ||
"codestyle:check": "prettier src/ --check", | ||
"codestyle:fix": "prettier src/ --write" | ||
}, | ||
"dependencies": { | ||
"@azure/openai": "^1.0.0-beta.11", | ||
"@instructor-ai/instructor": "^0.0.7", | ||
"@supabase/supabase-js": "^2.39.7", | ||
"axios": "^1.6.7", | ||
"dataclass": "^2.1.1", | ||
"js-yaml": "^4.1.0", | ||
"lodash": "^4.17.21", | ||
"openai": "^4.28.0", | ||
"remeda": "^1.44.1", | ||
"typesense": "^1.7.2", | ||
"zod": "^3.22.4", | ||
"zod-to-json-schema": "^3.22.4" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,35 @@ | ||
import { envVar } from '../general'; | ||
|
||
import { envVar } from "../general"; | ||
|
||
type TypesenseNodeConfig = { | ||
host: string; | ||
port: number; | ||
protocol: string; | ||
host: string; | ||
port: number; | ||
protocol: string; | ||
}; | ||
|
||
type TypesenseServiceConfig = { | ||
nodes: TypesenseNodeConfig[] | ||
connectionTimeoutSec: number; | ||
apiKey: string; | ||
docsCollection: string; | ||
docsSearchPhraseCollection: string; | ||
nodes: TypesenseNodeConfig[]; | ||
connectionTimeoutSec: number; | ||
apiKey: string; | ||
docsCollection: string; | ||
docsSearchPhraseCollection: string; | ||
}; | ||
|
||
|
||
export function typesenseConfig(): TypesenseServiceConfig { | ||
|
||
|
||
const cfg: TypesenseServiceConfig = { | ||
nodes: [{ | ||
host: envVar("TYPESENSE_API_HOST"), | ||
port: 443, protocol: "https" | ||
}], | ||
apiKey: envVar("TYPESENSE_API_KEY"), | ||
docsCollection: envVar("TYPESENSE_DOCS_COLLECTION"), | ||
docsSearchPhraseCollection: envVar("TYPESENSE_DOCS_SEARCH_PHRASE_COLLECTION"), | ||
connectionTimeoutSec: 2 | ||
} | ||
|
||
return cfg; | ||
} | ||
const cfg: TypesenseServiceConfig = { | ||
nodes: [ | ||
{ | ||
host: envVar("TYPESENSE_API_HOST"), | ||
port: 443, | ||
protocol: "https", | ||
}, | ||
], | ||
apiKey: envVar("TYPESENSE_API_KEY"), | ||
docsCollection: envVar("TYPESENSE_DOCS_COLLECTION"), | ||
docsSearchPhraseCollection: envVar( | ||
"TYPESENSE_DOCS_SEARCH_PHRASE_COLLECTION", | ||
), | ||
connectionTimeoutSec: 2, | ||
}; | ||
|
||
return cfg; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,75 +1,84 @@ | ||
import Instructor from '@instructor-ai/instructor'; | ||
import { z } from 'zod'; | ||
import Instructor from "@instructor-ai/instructor"; | ||
import { z } from "zod"; | ||
import { | ||
// azure_client, | ||
openaiClient | ||
} from '../llm'; | ||
import { scopedEnvVar } from '../general'; | ||
// azure_client, | ||
openaiClient, | ||
} from "../llm"; | ||
import { scopedEnvVar } from "../general"; | ||
|
||
const stage_name = 'DOCS_QA_EXTRACT'; | ||
const stage_name = "DOCS_QA_EXTRACT"; | ||
const envVar = scopedEnvVar(stage_name); | ||
|
||
// const azureClient = azure_client(); | ||
const openaiClientInstance = Instructor({ | ||
client: openaiClient() as any, | ||
mode: "FUNCTIONS", | ||
debug: envVar('DEBUG_INSTRUCTOR') | ||
client: openaiClient() as any, | ||
mode: "FUNCTIONS", | ||
debug: envVar("DEBUG_INSTRUCTOR"), | ||
}); | ||
|
||
const QueryRelaxationSchema = z.object({ | ||
searchQueries: z.array(z.string()) | ||
searchQueries: z.array(z.string()), | ||
}); | ||
|
||
export type QueryRelaxation = z.infer<typeof QueryRelaxationSchema> | null; | ||
|
||
export async function queryRelaxation(user_input: string): Promise<QueryRelaxation> { | ||
let query_result: QueryRelaxation | null = null; | ||
export async function queryRelaxation( | ||
user_input: string, | ||
): Promise<QueryRelaxation> { | ||
let query_result: QueryRelaxation | null = null; | ||
|
||
const prompt = `You have access to a search API that returns relevant documentation. | ||
const prompt = `You have access to a search API that returns relevant documentation. | ||
Your task is to generate an array of up to 7 search queries that are relevant to this question. | ||
Use a variation of related keywords and synonyms for the queries, trying to be as general as possible. | ||
Include as many queries as you can think of, including and excluding terms. | ||
For example, include queries like ['keyword_1 keyword_2', 'keyword_1', 'keyword_2']. | ||
Be creative. The more queries you include, the more likely you are to find relevant results.` | ||
Be creative. The more queries you include, the more likely you are to find relevant results.`; | ||
|
||
if (envVar('USE_AZURE_OPENAI_API', false) == true) { | ||
// query_result = await azureClient.chat.completions.create({ | ||
// model: envVar('AZURE_OPENAI_DEPLOYMENT'), | ||
// response_model: { schema: SearchQueriesSchema, name: "GeneratedSearchQueries" }, | ||
// temperature: 0.1, | ||
// max_retries: 0, | ||
// messages: [ | ||
// { | ||
// role: "system", | ||
// content: prompt }, | ||
// { role: "user", content: "[User query]\n" + user_input }, | ||
// ] | ||
// }); | ||
} else { | ||
console.log(`${stage_name} model name: ${envVar('OPENAI_API_MODEL_NAME', "")}`); | ||
query_result = await openaiClientInstance.chat.completions.create({ | ||
model: envVar('OPENAI_API_MODEL_NAME'), | ||
response_model: { schema: QueryRelaxationSchema, name: "QueryRelaxation" }, | ||
temperature: 0.1, | ||
max_retries: 0, | ||
messages: [ | ||
{ | ||
role: "system", | ||
content: prompt | ||
}, | ||
{ role: "user", content: "[User query]\n" + user_input }, | ||
] | ||
}); | ||
} | ||
if (envVar("USE_AZURE_OPENAI_API", false) == true) { | ||
// query_result = await azureClient.chat.completions.create({ | ||
// model: envVar('AZURE_OPENAI_DEPLOYMENT'), | ||
// response_model: { schema: SearchQueriesSchema, name: "GeneratedSearchQueries" }, | ||
// temperature: 0.1, | ||
// max_retries: 0, | ||
// messages: [ | ||
// { | ||
// role: "system", | ||
// content: prompt }, | ||
// { role: "user", content: "[User query]\n" + user_input }, | ||
// ] | ||
// }); | ||
} else { | ||
console.log( | ||
`${stage_name} model name: ${envVar("OPENAI_API_MODEL_NAME", "")}`, | ||
); | ||
query_result = await openaiClientInstance.chat.completions.create({ | ||
model: envVar("OPENAI_API_MODEL_NAME"), | ||
response_model: { | ||
schema: QueryRelaxationSchema, | ||
name: "QueryRelaxation", | ||
}, | ||
temperature: 0.1, | ||
max_retries: 0, | ||
messages: [ | ||
{ | ||
role: "system", | ||
content: prompt, | ||
}, | ||
{ role: "user", content: "[User query]\n" + user_input }, | ||
], | ||
}); | ||
} | ||
|
||
if (!query_result) { | ||
return null | ||
} | ||
if (!query_result) { | ||
return null; | ||
} | ||
|
||
for (let i = 0; i < query_result.searchQueries.length; i++) { | ||
query_result.searchQueries[i] = query_result.searchQueries[i].replace("GitHub", "").trim(); | ||
} | ||
for (let i = 0; i < query_result.searchQueries.length; i++) { | ||
query_result.searchQueries[i] = query_result.searchQueries[i] | ||
.replace("GitHub", "") | ||
.trim(); | ||
} | ||
|
||
return query_result; | ||
} | ||
return query_result; | ||
} |
Oops, something went wrong.