Skip to content

Commit 98781a5

Browse files
committed
📦 NEW: memory refresh
1 parent ddc937f commit 98781a5

File tree

6 files changed

+162
-17
lines changed

6 files changed

+162
-17
lines changed

baseai/memory/docs/index.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { MemoryI } from '@baseai/core';
22

33
const memoryDocs = (): MemoryI => ({
4-
name: 'docs',
5-
description: 'Docs folder of sourcegraph docs repository as an auto-synced memory',
4+
name: 'memory-sg-docs-live',
5+
description: 'An AI memory storing all Sourcegraph docs.',
66
git: {
77
enabled: true,
88
include: ['**/*.mdx'],
99
gitignore: true,
10-
deployedAt: '5f3fec8530280d01a783aadcdeb0ccc3f9cd8b70',
11-
embeddedAt: ''
10+
embeddedAt: '',
11+
deployedAt: '',
1212
},
1313
documents: {
1414
meta: doc => {
@@ -22,3 +22,6 @@ const memoryDocs = (): MemoryI => ({
2222
});
2323

2424
export default memoryDocs;
25+
26+
// Old
27+
// deployedAt: '5f3fec8530280d01a783aadcdeb0ccc3f9cd8b70',

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
"github-slugger": "^2.0.0",
4343
"js-yaml": "^4.1.0",
4444
"kbar": "^0.1.0-beta.44",
45-
"langbase": "^1.1.26",
45+
"langbase": "^1.1.46",
4646
"lucide-react": "^0.372.0",
4747
"mdx": "^0.3.1",
4848
"next": "^14.2.3",
@@ -68,6 +68,7 @@
6868
},
6969
"devDependencies": {
7070
"baseai": "^0.9.40",
71+
"dotenv": "^16.4.7",
7172
"eslint": "8.45.0",
7273
"eslint-config-next": "13.4.16",
7374
"glob": "^11.0.1",

pnpm-lock.yaml

Lines changed: 19 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/langbase-create-memory.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import 'dotenv/config';
2+
import { Langbase } from 'langbase';
3+
4+
const langbase = new Langbase({
5+
apiKey: process.env.LANGBASE_API_KEY!,
6+
});
7+
8+
async function main() {
9+
const memory = await langbase.memories.create({
10+
name: 'memory-sg-docs-live',
11+
description: 'An AI memory storing all Sourcegraph docs.',
12+
chunk_size: 10000,
13+
});
14+
15+
console.log('Memory created:', memory);
16+
}
17+
18+
main();

scripts/word-count.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
const path = require('path');
2+
const fs = require('fs');
3+
const {sync: glob} = require('glob');
4+
5+
// Configuration - Update this base URL as needed
6+
const baseUrl = 'https://sourcegraph.com/';
7+
8+
// Find all .mdx files, ignoring node_modules
9+
const mdxFiles = glob('**/*.mdx', {ignore: ['node_modules/**']});
10+
11+
// Array to store word counts
12+
const wordCounts = [];
13+
let totalWords = 0;
14+
15+
// Process each file
16+
mdxFiles.forEach(filePath => {
17+
const fileName = path.basename(filePath);
18+
const content = fs.readFileSync(filePath, 'utf8');
19+
20+
// Count words (split by whitespace)
21+
const words = content
22+
.trim()
23+
.split(/\s+/)
24+
.filter(word => word.length > 0);
25+
const wordCount = words.length;
26+
27+
// Add to total and array
28+
totalWords += wordCount;
29+
wordCounts.push({
30+
fileName,
31+
filePath,
32+
wordCount
33+
});
34+
35+
// Log word count for each file
36+
console.log(`${filePath}: ${wordCount} words`);
37+
});
38+
39+
// Calculate average
40+
const averageWordCount = totalWords / wordCounts.length;
41+
42+
// Calculate median (need to sort the array first)
43+
const sortedCounts = [...wordCounts].sort((a, b) => a.wordCount - b.wordCount);
44+
const middleIndex = Math.floor(sortedCounts.length / 2);
45+
const medianWordCount =
46+
sortedCounts.length % 2 === 0
47+
? (sortedCounts[middleIndex - 1].wordCount +
48+
sortedCounts[middleIndex].wordCount) /
49+
2
50+
: sortedCounts[middleIndex].wordCount;
51+
52+
// Log summary statistics
53+
console.log('\n--- Summary Statistics ---');
54+
console.log(`Total files: ${wordCounts.length}`);
55+
console.log(`Total words: ${totalWords}`);
56+
console.log(`Average words per file: ${averageWordCount.toFixed(2)}`);
57+
console.log(`Median words per file: ${medianWordCount}`);
58+
59+
// Find shortest and longest files
60+
const shortestFile = sortedCounts[0];
61+
const longestFile = sortedCounts[sortedCounts.length - 1];
62+
console.log(
63+
`\nShortest file: ${shortestFile.filePath} (${shortestFile.wordCount} words)`
64+
);
65+
console.log(
66+
`Longest file: ${longestFile.filePath} (${longestFile.wordCount} words)`
67+
);
68+
69+
// Optional: Write summary to a file
70+
const summary = {
71+
totalFiles: wordCounts.length,
72+
totalWords,
73+
averageWordCount,
74+
medianWordCount,
75+
fileCounts: wordCounts
76+
};
77+
78+
// fs.writeFileSync(
79+
// 'word-count-summary.json',
80+
// JSON.stringify(summary, null, 2),
81+
// 'utf8'
82+
// );
83+
console.log('\nSummary written to word-count-summary.json');

src/app/api/chat/route.ts

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,41 @@ const langbase = new Langbase({
99

1010
export async function POST(req: NextRequest) {
1111
const options = await req.json();
12+
console.log("🚀 ~ options:", options)
1213

13-
const { stream, threadId } = await langbase.pipe.run({
14-
...options,
15-
name: 'ask-sourcegraph-docs'
14+
const { completion } = await langbase.pipes.run({
15+
name: 'query-rewrite',
16+
messages: [
17+
{
18+
role: 'user',
19+
content: options.messages[options.messages.length - 1].content,
20+
},
21+
],
22+
stream: false
1623
});
24+
console.log("🚀 ~ completion:", completion)
25+
26+
27+
// Parse the completion to get the rewritten query
28+
const rewrittenQuery = JSON.parse(completion).rewrittenQuery;
29+
console.log("🚀 ~ rewrittenQuery:", rewrittenQuery)
30+
31+
// Create a new options object with the rewritten query
32+
const updatedOptions = {
33+
...options,
34+
messages: [
35+
...options.messages.slice(0, -1), // Keep all messages except the last one
36+
{
37+
role: 'user',
38+
content: rewrittenQuery
39+
}
40+
],
41+
name: 'ask-sourcegraph-docs',
42+
lastMessageOnly: true,
43+
};
44+
console.log("🚀 ~ updatedOptions:", updatedOptions)
45+
46+
const { stream, threadId } = await langbase.pipes.run(updatedOptions);
1747

1848
return new Response(stream, {
1949
status: 200,

0 commit comments

Comments
 (0)