chore: import upstream snapshot with attribution
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
# Basic Usage Example
|
||||
|
||||
This example demonstrates the basic usage of Claude Context.
|
||||
|
||||
## Prerequisites
|
||||
|
||||
1. **OpenAI API Key**: Set your OpenAI API key for embeddings:
|
||||
```bash
|
||||
export OPENAI_API_KEY="your-openai-api-key"
|
||||
```
|
||||
|
||||
2. **Milvus Server**: Make sure Milvus server is running:
|
||||
- You can also use fully managed Milvus on [Zilliz Cloud](https://zilliz.com/cloud).
|
||||
In this case, set the `MILVUS_ADDRESS` as the Public Endpoint and `MILVUS_TOKEN` as the Token like this:
|
||||
```bash
|
||||
export MILVUS_ADDRESS="https://your-cluster.zillizcloud.com"
|
||||
export MILVUS_TOKEN="your-zilliz-token"
|
||||
```
|
||||
|
||||
|
||||
- You can also set up a Milvus server on [Docker or Kubernetes](https://milvus.io/docs/install-overview.md). In this setup, please use the server address and port as your `uri`, e.g.`http://localhost:19530`. If you enable the authentication feature on Milvus, set the `token` as `"<your_username>:<your_password>"`, otherwise there is no need to set the token.
|
||||
```bash
|
||||
export MILVUS_ADDRESS="http://localhost:19530"
|
||||
export MILVUS_TOKEN="<your_username>:<your_password>"
|
||||
```
|
||||
|
||||
|
||||
## Running the Example
|
||||
|
||||
1. Install dependencies:
|
||||
```bash
|
||||
pnpm install
|
||||
```
|
||||
|
||||
2. Set environment variables (see examples above)
|
||||
|
||||
3. Run the example:
|
||||
```bash
|
||||
pnpm run start
|
||||
```
|
||||
|
||||
## What This Example Does
|
||||
1. **Indexes Codebase**: Indexes the entire Claude Context project
|
||||
2. **Performs Searches**: Executes semantic searches for different code patterns
|
||||
3. **Shows Results**: Displays search results with similarity scores and file locations
|
||||
|
||||
## Expected Output
|
||||
|
||||
```
|
||||
🚀 Claude Context Real Usage Example
|
||||
===============================
|
||||
...
|
||||
🔌 Connecting to vector database at: ...
|
||||
|
||||
📖 Starting to index codebase...
|
||||
🗑️ Existing index found, clearing it first...
|
||||
📊 Indexing stats: 45 files, 234 code chunks
|
||||
|
||||
🔍 Performing semantic search...
|
||||
|
||||
🔎 Search: "vector database operations"
|
||||
1. Similarity: 89.23%
|
||||
File: /path/to/packages/core/src/vectordb/milvus-vectordb.ts
|
||||
Language: typescript
|
||||
Lines: 147-177
|
||||
Preview: async search(collectionName: string, queryVector: number[], options?: SearchOptions)...
|
||||
|
||||
🎉 Example completed successfully!
|
||||
```
|
||||
@@ -0,0 +1,136 @@
|
||||
import { Context, MilvusVectorDatabase, MilvusRestfulVectorDatabase, AstCodeSplitter, LangChainCodeSplitter } from '@zilliz/claude-context-core';
|
||||
import { envManager } from '@zilliz/claude-context-core';
|
||||
import * as path from 'path';
|
||||
|
||||
// Try to load .env file
|
||||
try {
|
||||
require('dotenv').config();
|
||||
} catch (error) {
|
||||
// dotenv is not required, skip if not installed
|
||||
}
|
||||
|
||||
async function main() {
|
||||
console.log('🚀 Context Real Usage Example');
|
||||
console.log('===============================');
|
||||
|
||||
try {
|
||||
// 1. Choose Vector Database implementation
|
||||
// Set to true to use RESTful API (for environments without gRPC support)
|
||||
// Set to false to use gRPC (default, more efficient)
|
||||
const useRestfulApi = false;
|
||||
const milvusAddress = envManager.get('MILVUS_ADDRESS') || 'localhost:19530';
|
||||
const milvusToken = envManager.get('MILVUS_TOKEN');
|
||||
const splitterType = envManager.get('SPLITTER_TYPE')?.toLowerCase() || 'ast';
|
||||
|
||||
console.log(`🔧 Using ${useRestfulApi ? 'RESTful API' : 'gRPC'} implementation`);
|
||||
console.log(`🔌 Connecting to Milvus at: ${milvusAddress}`);
|
||||
|
||||
let vectorDatabase;
|
||||
if (useRestfulApi) {
|
||||
// Use RESTful implementation (for environments without gRPC support)
|
||||
vectorDatabase = new MilvusRestfulVectorDatabase({
|
||||
address: milvusAddress,
|
||||
...(milvusToken && { token: milvusToken })
|
||||
});
|
||||
} else {
|
||||
// Use gRPC implementation (default, more efficient)
|
||||
vectorDatabase = new MilvusVectorDatabase({
|
||||
address: milvusAddress,
|
||||
...(milvusToken && { token: milvusToken })
|
||||
});
|
||||
}
|
||||
|
||||
// 2. Create Context instance
|
||||
let codeSplitter;
|
||||
if (splitterType === 'langchain') {
|
||||
codeSplitter = new LangChainCodeSplitter(1000, 200);
|
||||
} else {
|
||||
codeSplitter = new AstCodeSplitter(2500, 300);
|
||||
}
|
||||
const context = new Context({
|
||||
vectorDatabase,
|
||||
codeSplitter,
|
||||
supportedExtensions: ['.ts', '.js', '.py', '.java', '.cpp', '.go', '.rs']
|
||||
});
|
||||
|
||||
// 3. Check if index already exists and clear if needed
|
||||
console.log('\n📖 Starting to index codebase...');
|
||||
const codebasePath = path.join(__dirname, '../..'); // Index entire project
|
||||
|
||||
// Check if index already exists
|
||||
const hasExistingIndex = await context.hasIndex(codebasePath);
|
||||
if (hasExistingIndex) {
|
||||
console.log('🗑️ Existing index found, clearing it first...');
|
||||
await context.clearIndex(codebasePath);
|
||||
}
|
||||
|
||||
// Index with progress tracking
|
||||
const indexStats = await context.indexCodebase(codebasePath);
|
||||
|
||||
// 4. Show indexing statistics
|
||||
console.log(`\n📊 Indexing stats: ${indexStats.indexedFiles} files, ${indexStats.totalChunks} code chunks`);
|
||||
|
||||
// 5. Perform semantic search
|
||||
console.log('\n🔍 Performing semantic search...');
|
||||
|
||||
const queries = [
|
||||
'vector database operations',
|
||||
'code splitting functions',
|
||||
'embedding generation',
|
||||
'typescript interface definitions'
|
||||
];
|
||||
|
||||
for (const query of queries) {
|
||||
console.log(`\n🔎 Search: "${query}"`);
|
||||
const results = await context.semanticSearch(codebasePath, query, 3, 0.3);
|
||||
|
||||
if (results.length > 0) {
|
||||
results.forEach((result, index) => {
|
||||
console.log(` ${index + 1}. Similarity: ${(result.score * 100).toFixed(2)}%`);
|
||||
console.log(` File: ${path.join(codebasePath, result.relativePath)}`);
|
||||
console.log(` Language: ${result.language}`);
|
||||
console.log(` Lines: ${result.startLine}-${result.endLine}`);
|
||||
console.log(` Preview: ${result.content.substring(0, 100)}...`);
|
||||
});
|
||||
} else {
|
||||
console.log(' No relevant results found');
|
||||
}
|
||||
}
|
||||
|
||||
console.log('\n🎉 Example completed successfully!');
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ Error occurred:', error);
|
||||
|
||||
// Provide detailed error diagnostics
|
||||
if (error instanceof Error) {
|
||||
if (error.message.includes('API key')) {
|
||||
console.log('\n💡 Please make sure to set the correct OPENAI_API_KEY environment variable');
|
||||
console.log(' Example: export OPENAI_API_KEY="your-actual-api-key"');
|
||||
} else if (error.message.includes('Milvus') || error.message.includes('connect')) {
|
||||
console.log('\n💡 Please make sure Milvus service is running');
|
||||
console.log(' - Default address: localhost:19530');
|
||||
console.log(' - Can be modified via MILVUS_ADDRESS environment variable');
|
||||
console.log(' - For RESTful API: set MILVUS_USE_RESTFUL=true');
|
||||
console.log(' - For gRPC (default): set MILVUS_USE_RESTFUL=false or leave unset');
|
||||
console.log(' - Start Milvus: docker run -p 19530:19530 milvusdb/milvus:latest');
|
||||
}
|
||||
|
||||
console.log('\n💡 Environment Variables:');
|
||||
console.log(' - OPENAI_API_KEY: Your OpenAI API key (required)');
|
||||
console.log(' - OPENAI_BASE_URL: Custom OpenAI API endpoint (optional)');
|
||||
console.log(' - MILVUS_ADDRESS: Milvus server address (default: localhost:19530)');
|
||||
console.log(' - MILVUS_TOKEN: Milvus authentication token (optional)');
|
||||
console.log(' - SPLITTER_TYPE: Code splitter type - "ast" or "langchain" (default: ast)');
|
||||
}
|
||||
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
// Run main program
|
||||
if (require.main === module) {
|
||||
main().catch(console.error);
|
||||
}
|
||||
|
||||
export { main };
|
||||
@@ -0,0 +1,21 @@
|
||||
{
|
||||
"name": "claude-context-basic-example",
|
||||
"version": "0.1.3",
|
||||
"description": "Basic usage example for Claude Context",
|
||||
"main": "index.ts",
|
||||
"scripts": {
|
||||
"start": "tsx index.ts",
|
||||
"build": "tsc index.ts --outDir dist --target es2020 --module commonjs --moduleResolution node --esModuleInterop true",
|
||||
"dev": "tsx --watch index.ts"
|
||||
},
|
||||
"dependencies": {
|
||||
"@zilliz/claude-context-core": "workspace:*"
|
||||
},
|
||||
"devDependencies": {
|
||||
"tsx": "^4.0.0",
|
||||
"typescript": "^5.0.0",
|
||||
"@types/node": "^20.0.0",
|
||||
"dotenv": "^16.0.0"
|
||||
},
|
||||
"private": true
|
||||
}
|
||||
Reference in New Issue
Block a user