Skip to content

Commit

Permalink
feat: split cv files into txt files for easier management
Browse files Browse the repository at this point in the history
  • Loading branch information
dbil committed Jan 6, 2025
1 parent bd70dd5 commit 0bf08a7
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 22 deletions.
12 changes: 8 additions & 4 deletions .github/workflows/main_func-cv-chatbot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,17 @@ jobs:
with:
node-version: ${{ env.NODE_VERSION }}

- name: 'Resolve Project Dependencies Using Npm'
shell: pwsh
- name: 'Build and Package'
shell: bash
run: |
pushd './${{ env.AZURE_FUNCTIONAPP_PACKAGE_PATH }}'
npm install
npm run build --if-present
npm run test --if-present
npm run build
# Create directory and copy files (using Windows-compatible commands)
mkdir -p dist/cv-chatbot-backend/cv-files
cp -r cv-chatbot-backend/cv-files/* dist/cv-chatbot-backend/cv-files/
# List contents for debugging
ls -la dist/cv-chatbot-backend/cv-files/
popd
- name: Upload artifact for deployment job
Expand Down
44 changes: 44 additions & 0 deletions backend/cv-chatbot-backend/copyFiles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import * as fs from 'fs';
import * as path from 'path';

// Get the project root directory (backend folder)
const rootDir = path.resolve(__dirname, '../..');
console.log('Root directory:', rootDir);

// Define source and target directories
const sourceDir = path.join(rootDir, 'cv-chatbot-backend', 'cv-files');
const targetDir = path.join(rootDir, 'dist', 'cv-chatbot-backend', 'cv-files');

try {
console.log('Source directory:', sourceDir);
console.log('Target directory:', targetDir);

// Create target directory if it doesn't exist
if (!fs.existsSync(targetDir)) {
fs.mkdirSync(targetDir, { recursive: true });
}

// Check if source directory exists
if (!fs.existsSync(sourceDir)) {
console.error(`Source directory not found: ${sourceDir}`);
console.error('Current directory:', __dirname);
process.exit(1);
}

// Copy all .txt files
const files = fs.readdirSync(sourceDir);
files.forEach(file => {
if (file.endsWith('.txt')) {
const sourcePath = path.join(sourceDir, file);
const targetPath = path.join(targetDir, file);
fs.copyFileSync(sourcePath, targetPath);
console.log(`Copied: ${file}`);
}
});

console.log('CV files copied successfully!');
} catch (error) {
console.error('Error copying files:', error);
console.error('Current directory:', __dirname);
process.exit(1);
}
4 changes: 4 additions & 0 deletions backend/cv-chatbot-backend/cv-files/experience.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Software Engineer at Company X (2020-Present)
- Led development of microservices architecture
- Implemented CI/CD pipelines
- Mentored junior developers
9 changes: 9 additions & 0 deletions backend/cv-chatbot-backend/cv-files/skills.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Programming Languages:
- TypeScript/JavaScript
- Python
- Java

Technologies:
- Azure Cloud Services
- Docker
- Kubernetes
5 changes: 4 additions & 1 deletion backend/cv-chatbot-backend/function.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,8 @@
"name": "res"
}
],
"scriptFile": "../dist/cv-chatbot-backend/index.js"
"scriptFile": "../dist/cv-chatbot-backend/index.js",
"files": [
"cv-files/**/*.txt"
]
}
69 changes: 54 additions & 15 deletions backend/cv-chatbot-backend/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,48 @@
import { AzureFunction, Context, HttpRequest } from "@azure/functions";
import axios from 'axios';
import * as fs from 'fs';
import * as path from 'path';

const CV_TEXT = `
David is a software engineer with X years of experience.
He has worked on projects using Y, Z technologies.
David is proficient in A, B, and C, and is currently based in Germany.
`;
// Function to read all .txt files from a directory
async function loadCVTexts(): Promise<string> {
try {
// Try multiple possible locations for the cv-files directory
const possiblePaths = [
path.join(__dirname, 'cv-files'),
path.join(process.cwd(), 'cv-files'),
path.join(process.cwd(), 'dist', 'cv-chatbot-backend', 'cv-files')
];

let cvPath = '';
for (const p of possiblePaths) {
if (fs.existsSync(p)) {
cvPath = p;
break;
}
}

if (!cvPath) {
throw new Error(`CV files directory not found. Tried paths: ${possiblePaths.join(', ')}`);
}

console.log('Using CV files from:', cvPath);
const files = fs.readdirSync(cvPath);
const txtFiles = files.filter(file => file.endsWith('.txt'));

let combinedText = '';
for (const file of txtFiles) {
const filePath = path.join(cvPath, file);
const content = fs.readFileSync(filePath, 'utf8');
combinedText += `=== ${file} ===\n${content}\n\n`;
}
return combinedText.trim();
} catch (error) {
console.error('Error reading CV files:', error);
console.error('Current directory:', __dirname);
console.error('Working directory:', process.cwd());
throw error;
}
}

const httpTrigger: AzureFunction = async function (context: Context, req: HttpRequest): Promise<void> {
const headers = {
Expand All @@ -29,18 +66,20 @@ const httpTrigger: AzureFunction = async function (context: Context, req: HttpRe
headers
};

const userQuery = req.body?.query || "";
try {
// Read CV texts from the cv-files directory
const CV_TEXT = await loadCVTexts();

const userQuery = req.body?.query || "";

// Combine CV and user question into one prompt
const prompt = `
You are an AI assistant knowledgeable about David's career.
Here is David's CV:
"${CV_TEXT}"
The user asked: "${userQuery}"
Please provide a helpful and professional answer.
`;
const prompt = `
You are an AI assistant knowledgeable about David's career.
Here is David's CV:
"${CV_TEXT}"
The user asked: "${userQuery}"
Please provide a helpful and professional answer.
`;

try {
// Call OpenAI or Azure OpenAI Chat Completion
const response = await axios.post(
"https://api.openai.com/v1/chat/completions",
Expand Down
3 changes: 2 additions & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"version": "1.0.0",
"description": "Azure Function",
"scripts": {
"build": "tsc",
"prebuild": "rm -rf dist",
"build": "tsc && node dist/cv-chatbot-backend/copyFiles.js",
"watch": "tsc -w",
"prestart": "npm run build",
"start": "func start",
Expand Down
5 changes: 4 additions & 1 deletion backend/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@
"sourceMap": true,
"strict": true,
"esModuleInterop": true
}
},
"include": [
"cv-chatbot-backend/**/*"
]
}

0 comments on commit 0bf08a7

Please sign in to comment.