Skip to content

Commit

Permalink
FINNALY WORKING
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Jun 23, 2024
1 parent eb8e660 commit b2251fe
Show file tree
Hide file tree
Showing 16 changed files with 342 additions and 336 deletions.
6 changes: 5 additions & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ COHERE_API_TOKEN=#your cohere api token here
HF_ACCESS_TOKEN=#LEGACY! Use HF_TOKEN instead

# used to activate search with web functionality. disabled if none are defined. choose one of the following:
YDC_API_KEY=bec6be6c-87cd-45f8-b430-fc0fc3799ac4<__>1PTsFeETU8N2v5f4qmtDZVGS
YDC_API_KEY="bec6be6c-87cd-45f8-b430-fc0fc3799ac4<__>1PTsFeETU8N2v5f4qmtDZVGS"

#your docs.you.com api key here
SERPER_API_KEY=#your serper.dev api key here
Expand All @@ -27,6 +27,10 @@ SERPSTACK_API_KEY=#your serpstack api key here
USE_LOCAL_WEBSEARCH=#set to true to parse google results yourself, overrides other API keys
SEARXNG_QUERY_URL=http://ec2-50-18-222-32.us-west-1.compute.amazonaws.com/searxng/search?q=<query>&format=json

AWS_BEDROCK_ACCESS_KEY_ID="AKIAQ3EGTCOSHLW5Q2KT"

Check warning on line 30 in .env

View workflow job for this annotation

GitHub Actions / trufflehog

Found verified AWS result 🐷🔑
AWS_BEDROCK_SECRET_ACCESS_KEY="7tTeIgJKuu56ydHeY77ROEuQEh8fMl2Vgxa6cj3q"


WEBSEARCH_ALLOWLIST=`[]` # if it's defined, allow websites from only this list.
WEBSEARCH_BLOCKLIST=`[]` # if it's defined, block websites from this list.
WEBSEARCH_JAVASCRIPT=true # CPU usage reduces by 60% on average by disabling javascript. Enable to improve website compatibility
Expand Down
6 changes: 6 additions & 0 deletions .svelte-kit/ambient.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ declare module '$env/static/private' {
export const SERPSTACK_API_KEY: string;
export const USE_LOCAL_WEBSEARCH: string;
export const SEARXNG_QUERY_URL: string;
export const AWS_BEDROCK_ACCESS_KEY_ID: string;
export const AWS_BEDROCK_SECRET_ACCESS_KEY: string;
export const WEBSEARCH_ALLOWLIST: string;
export const WEBSEARCH_BLOCKLIST: string;
export const WEBSEARCH_JAVASCRIPT: string;
Expand Down Expand Up @@ -160,6 +162,7 @@ declare module '$env/static/private' {
export const USERDOMAIN_ROAMINGPROFILE: string;
export const USERNAME: string;
export const USERPROFILE: string;
export const VIPSHOME: string;
export const VSCODE_GIT_ASKPASS_EXTRA_ARGS: string;
export const VSCODE_GIT_ASKPASS_MAIN: string;
export const VSCODE_GIT_ASKPASS_NODE: string;
Expand Down Expand Up @@ -226,6 +229,8 @@ declare module '$env/dynamic/private' {
SERPSTACK_API_KEY: string;
USE_LOCAL_WEBSEARCH: string;
SEARXNG_QUERY_URL: string;
AWS_BEDROCK_ACCESS_KEY_ID: string;
AWS_BEDROCK_SECRET_ACCESS_KEY: string;
WEBSEARCH_ALLOWLIST: string;
WEBSEARCH_BLOCKLIST: string;
WEBSEARCH_JAVASCRIPT: string;
Expand Down Expand Up @@ -342,6 +347,7 @@ declare module '$env/dynamic/private' {
USERDOMAIN_ROAMINGPROFILE: string;
USERNAME: string;
USERPROFILE: string;
VIPSHOME: string;
VSCODE_GIT_ASKPASS_EXTRA_ARGS: string;
VSCODE_GIT_ASKPASS_MAIN: string;
VSCODE_GIT_ASKPASS_NODE: string;
Expand Down
2 changes: 1 addition & 1 deletion .svelte-kit/generated/server/internal.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"@xenova/transformers": "^2.16.1",
"autoprefixer": "^10.4.14",
"aws4": "^1.13.0",
"axios": "^1.7.2",
"browser-image-resizer": "^2.4.1",
"chrono-node": "^2.7.6",
"compromise": "^14.13.0",
Expand Down
4 changes: 2 additions & 2 deletions src/lib/components/chat/ChatIntroduction.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@
</div>
</div>
<p class="text-base text-gray-600 dark:text-gray-400">

DisasterAid.ai is an AI-powered platform that consolidates trustworthy live updates about ongoing crises and packages them into summarized info-bites.
DisasterAid.ai consolidates trustworthy live news updates from You.com about ongoing crises
and packages them into summarized info-bites.
</p>
</div>
</div>
Expand Down
101 changes: 54 additions & 47 deletions src/lib/server/textGeneration/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,52 +22,59 @@ import type { ToolResult } from "$lib/types/Tool";
import { toolHasName } from "../tools/utils";
import directlyAnswer from "../tools/directlyAnswer";

export async function* textGeneration(ctx: TextGenerationContext) {
yield* mergeAsyncGenerators([
textGenerationWithoutTitle(ctx),
generateTitleForConversation(ctx.conv),
]);
}
import process from "../websearch/tractor-extractor/you-search.js"
import invokeModel from "../websearch/tractor-extractor/aws.js"

import { SummaryAgentPrompt, evacuationCenters } from "./prompts.js";


export async function textGeneration(ctx: TextGenerationContext) {

const { conv, toolsPreference, messages, convId } = ctx;


const q = conv.messages.filter((m) => m.from === "user").map((m) => m.content).join("\n");


console.log(q)
const webSearchResult = await process(q)


const extractionString = JSON.stringify(webSearchResult)

console.log(extractionString)



const queryString =
SummaryAgentPrompt +
"Evacuation Centers: " +
JSON.stringify(evacuationCenters, null, 2) +
"NEWS: " +
extractionString;

let response = await invokeModel(queryString);

response = response?.output.message?.content[0]?.text;
console.log(response);

return response


// let preprompt = conv.preprompt;
// if (assistantHasDynamicPrompt(assistant) && preprompt) {
// preprompt = await processPreprompt(preprompt);
// if (messages[0].from === "system") messages[0].content = preprompt;
// }

// let toolResults: ToolResult[] = [];

// if (model.tools && !conv.assistantId) {
// const tools = pickTools(toolsPreference, Boolean(assistant));
// const toolCallsRequired = tools.some((tool) => !toolHasName(directlyAnswer.name, tool));
// if (toolCallsRequired) toolResults = yield* runTools(ctx, tools, preprompt);
// }

async function* textGenerationWithoutTitle(
ctx: TextGenerationContext
): AsyncGenerator<MessageUpdate, undefined, undefined> {
yield {
type: MessageUpdateType.Status,
status: MessageUpdateStatus.Started,
};

ctx.assistant ??= await getAssistantById(ctx.conv.assistantId);
const { model, conv, messages, assistant, isContinue, webSearch, toolsPreference } = ctx;
const convId = conv._id;

let webSearchResult: WebSearch | undefined;

// run websearch if:
// - it's not continuing a previous message
// - AND the model doesn't support tools and websearch is selected
// - OR the assistant has websearch enabled (no tools for assistants for now)
if (
!isContinue &&
((!model.tools && webSearch && !conv.assistantId) || assistantHasWebSearch(assistant))
) {
webSearchResult = yield* runWebSearch(conv, messages, assistant?.rag);
}

let preprompt = conv.preprompt;
if (assistantHasDynamicPrompt(assistant) && preprompt) {
preprompt = await processPreprompt(preprompt);
if (messages[0].from === "system") messages[0].content = preprompt;
}

let toolResults: ToolResult[] = [];

if (model.tools && !conv.assistantId) {
const tools = pickTools(toolsPreference, Boolean(assistant));
const toolCallsRequired = tools.some((tool) => !toolHasName(directlyAnswer.name, tool));
if (toolCallsRequired) toolResults = yield* runTools(ctx, tools, preprompt);
}

const processedMessages = await preprocessMessages(messages, webSearchResult, convId);
yield* generate({ ...ctx, messages: processedMessages }, toolResults, preprompt);
// const processedMessages = await preprocessMessages(messages, webSearchResult, convId);
// yield* generate({ ...ctx, messages: processedMessages }, toolResults, preprompt);
}
3 changes: 1 addition & 2 deletions src/lib/server/tools/web/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ const websearch: BackendTool = {
async *call({ query }, { conv, assistant, messages }) {
const webSearchToolResults = yield* runWebSearch(conv, messages, assistant?.rag, String(query));
const chunks = webSearchToolResults?.contextSources
.map(({ context }) => context)
.join("\n------------\n");


return {
outputs: [{ websearch: chunks }],
Expand Down
44 changes: 24 additions & 20 deletions src/lib/server/websearch/runWebSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
makeSourcesUpdate,
} from "./update";
import { mergeAsyncGenerators } from "$lib/utils/mergeAsyncGenerators";
import { MetricsServer } from "../metrics";
// import { MetricsServer } from "../metrics";

const MAX_N_PAGES_TO_SCRAPE = 3 as const;
const MAX_N_PAGES_TO_EMBED = 2 as const;
Expand All @@ -35,7 +35,7 @@ export async function* runWebSearch(
const createdAt = new Date();
const updatedAt = new Date();

MetricsServer.getMetrics().webSearch.requestCount.inc();
// MetricsServer.getMetrics().webSearch.requestCount.inc();

try {
const embeddingModel =
Expand Down Expand Up @@ -71,6 +71,8 @@ export async function* runWebSearch(
absoluteURLs: true,
});

console.log(extraction)

if (extraction && extraction.sentences?.length) {

// console.log(extraction["sorted_sentences"]);
Expand All @@ -92,32 +94,34 @@ export async function* runWebSearch(
allScrapedPages.push(extraction);
}

console.log(allScrapedPages)

const scrapedPages = allScrapedPages.slice(0, MAX_N_PAGES_TO_EMBED);


if (!scrapedPages.length) {
throw Error(`No text found in the first ${MAX_N_PAGES_TO_SCRAPE} results`);
}
const contextSources = JSON.stringify(allScrapedPages);


console.log(contextSources)

// if (!scrapedPages.length) {
// throw Error(`No text found in the first ${MAX_N_PAGES_TO_SCRAPE} results`);
// }

// Chunk the text of each of the elements and find the most similar chunks to the prompt
yield makeGeneralUpdate({ message: "Extracting relevant information" });
const contextSources = await findContextSources(scrapedPages, prompt, embeddingModel).then(
(ctxSources) =>
ctxSources.map((source) => ({
...source,
page: { ...source.page, markdownTree: removeParents(source.page.markdownTree) },
}))
);
yield makeSourcesUpdate(contextSources);
// // // Chunk the text of each of the elements and find the most similar chunks to the prompt
// // yield makeGeneralUpdate({ message: "Extracting relevant information" });
// // const contextSources = await findContextSources(scrapedPages, prompt, embeddingModel).then(
// // (ctxSources) =>
// // ctxSources.map((source) => ({
// // ...source,
// // page: { ...source.page, markdownTree: removeParents(source.page.markdownTree) },
// // }))
// // );
// // yield makeSourcesUpdate(contextSources);

const webSearch: WebSearch = {
prompt,
searchQuery,
results: scrapedPages.map(({ page, ...source }) => ({
...source,
page: { ...page, markdownTree: removeParents(page.markdownTree) },
})),
results: allScrapedPages,
contextSources,
createdAt,
updatedAt,
Expand Down
69 changes: 31 additions & 38 deletions src/lib/server/websearch/tractor-extractor/aws.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,39 @@
import {
BedrockRuntimeClient,
ConverseCommand,
} from "@aws-sdk/client-bedrock-runtime";
import dotenv from 'dotenv'
dotenv.config()
import { BedrockRuntimeClient, ConverseCommand } from "@aws-sdk/client-bedrock-runtime";
import { env } from "$env/dynamic/private";

const client = new BedrockRuntimeClient({
region: "us-east-1",
credentials: {
accessKeyId: process.env.AWS_BEDROCK_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_BEDROCK_SECRET_ACCESS_KEY,
},
region: "us-east-1",
credentials: {
accessKeyId: env.AWS_BEDROCK_ACCESS_KEY_ID,
secretAccessKey: env.AWS_BEDROCK_SECRET_ACCESS_KEY,
},
});

export default async function invokeModel(queryString) {
// Prepare the request parameters
const params = {
modelId: "anthropic.claude-3-5-sonnet-20240620-v1:0",

// Prepare the request parameters
const params = {
modelId: "anthropic.claude-3-5-sonnet-20240620-v1:0",

"max_tokens": 1024,
"messages": [
{
"role": "user",
"content": [

{
"type": "text",
"text": queryString
}
]
}
]
};
max_tokens: 1024,
messages: [
{
role: "user",
content: [
{
type: "text",
text: queryString,
},
],
},
],
};

const command = new ConverseCommand(params);

const command = new ConverseCommand(params);

try {
const data = await client.send(command);
return data;
} catch (error) {
console.error(error);
}
}
try {
const data = await client.send(command);
return data;
} catch (error) {
console.error(error);
}
}
Loading

0 comments on commit b2251fe

Please sign in to comment.