-
Notifications
You must be signed in to change notification settings - Fork 45
feat: Initialize LangChain with OpenAI integration and comprehensive API #76
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
morningstarxcdcode
wants to merge
1
commit into
NexGenStudioDev:master
Choose a base branch
from
morningstarxcdcode:feat/issue-20-langchain-init
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
Large diffs are not rendered by default.
Oops, something went wrong.
173 changes: 173 additions & 0 deletions
173
LocalMind-Backend/src/api/v1/LangChain/langchain.controller.ts
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,173 @@ | ||
| import { Request, Response } from 'express' | ||
| import { SendResponse } from '../utils/SendResponse.utils' | ||
| import langchainService from '../services/langchain.service' | ||
morningstarxcdcode marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * LangChain Controller | ||
| * | ||
| * Handles HTTP requests for LangChain-powered AI operations. | ||
| * Provides endpoints for: | ||
| * - Simple chat with system + user prompts | ||
| * - User-only prompts | ||
| * - Custom template execution | ||
| * - Streaming responses | ||
| */ | ||
| class LangChainController { | ||
| /** | ||
| * Simple chat endpoint with system and user prompts | ||
| * | ||
| * POST /api/v1/langchain/chat | ||
| * Body: { systemPrompt: string, userPrompt: string } | ||
| */ | ||
| async chat(req: Request, res: Response) { | ||
| try { | ||
| const { systemPrompt, userPrompt } = req.body | ||
|
|
||
| if (!userPrompt) { | ||
| return SendResponse.error(res, 'userPrompt is required', 400) | ||
| } | ||
|
|
||
| const defaultSystemPrompt = | ||
| systemPrompt || 'You are a helpful AI assistant powered by LocalMind.' | ||
|
|
||
| const response = await langchainService.runSimplePrompt(defaultSystemPrompt, userPrompt) | ||
|
|
||
| SendResponse.success( | ||
| res, | ||
| 'AI response generated successfully', | ||
| { | ||
| response, | ||
| systemPrompt: defaultSystemPrompt, | ||
| userPrompt, | ||
| }, | ||
| 200 | ||
| ) | ||
| } catch (error: any) { | ||
| console.error('LangChain chat error:', error) | ||
| SendResponse.error(res, 'Failed to generate AI response', 500, { error: error.message }) | ||
morningstarxcdcode marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
morningstarxcdcode marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
morningstarxcdcode marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * User prompt only (no system message) | ||
| * | ||
| * POST /api/v1/langchain/prompt | ||
| * Body: { prompt: string } | ||
| */ | ||
| async prompt(req: Request, res: Response) { | ||
| try { | ||
| const { prompt } = req.body | ||
|
|
||
| if (!prompt) { | ||
| return SendResponse.error(res, 'prompt is required', 400) | ||
| } | ||
|
|
||
| const response = await langchainService.runUserPrompt(prompt) | ||
|
|
||
| SendResponse.success( | ||
| res, | ||
| 'AI response generated successfully', | ||
| { | ||
| response, | ||
| prompt, | ||
| }, | ||
| 200 | ||
| ) | ||
| } catch (error: any) { | ||
| console.error('LangChain prompt error:', error) | ||
| SendResponse.error(res, 'Failed to generate AI response', 500, { error: error.message }) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Custom template with variables | ||
| * | ||
| * POST /api/v1/langchain/template | ||
| * Body: { template: string, variables: object } | ||
| */ | ||
| async customTemplate(req: Request, res: Response) { | ||
| try { | ||
| const { template, variables } = req.body | ||
|
|
||
| if (!template) { | ||
| return SendResponse.error(res, 'template is required', 400) | ||
| } | ||
|
|
||
| if (!variables || typeof variables !== 'object') { | ||
| return SendResponse.error(res, 'variables must be an object', 400) | ||
| } | ||
|
|
||
| const response = await langchainService.runCustomTemplate(template, variables) | ||
|
|
||
| SendResponse.success( | ||
| res, | ||
| 'Template executed successfully', | ||
| { | ||
| response, | ||
| template, | ||
| variables, | ||
| }, | ||
| 200 | ||
| ) | ||
| } catch (error: any) { | ||
| console.error('LangChain template error:', error) | ||
| SendResponse.error(res, 'Failed to execute template', 500, { error: error.message }) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Health check endpoint to verify LangChain is configured | ||
| * | ||
| * GET /api/v1/langchain/health | ||
| */ | ||
| async healthCheck(req: Request, res: Response) { | ||
| try { | ||
| const model = langchainService.getChatModel() | ||
|
|
||
| SendResponse.success( | ||
| res, | ||
| 'LangChain is configured and ready', | ||
| { | ||
| status: 'operational', | ||
| model: model.modelName, | ||
| temperature: model.temperature, | ||
| maxTokens: model.maxTokens, | ||
| }, | ||
morningstarxcdcode marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 200 | ||
| ) | ||
| } catch (error: any) { | ||
| SendResponse.error(res, 'LangChain is not properly configured', 500, { error: error.message }) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Test endpoint with a simple query | ||
| * | ||
| * GET /api/v1/langchain/test | ||
| */ | ||
| async test(req: Request, res: Response) { | ||
| try { | ||
| const testPrompt = 'Say hello in one sentence and confirm you are working correctly.' | ||
|
|
||
| const response = await langchainService.runSimplePrompt( | ||
| 'You are a helpful AI assistant.', | ||
| testPrompt | ||
| ) | ||
|
|
||
| SendResponse.success( | ||
| res, | ||
| 'LangChain test successful', | ||
| { | ||
| testPrompt, | ||
| response, | ||
| timestamp: new Date().toISOString(), | ||
| }, | ||
| 200 | ||
| ) | ||
| } catch (error: any) { | ||
| SendResponse.error(res, 'LangChain test failed', 500, { error: error.message }) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| export default new LangChainController() | ||
27 changes: 27 additions & 0 deletions
27
LocalMind-Backend/src/api/v1/LangChain/langchain.routes.ts
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import { Router } from 'express' | ||
| import langchainController from './langchain.controller' | ||
|
|
||
| const router: Router = Router() | ||
|
|
||
| /** | ||
| * LangChain Routes | ||
| * | ||
| * Endpoints for LangChain-powered AI operations | ||
| */ | ||
|
|
||
| // Health check | ||
| router.get('/v1/langchain/health', langchainController.healthCheck) | ||
|
|
||
| // Test endpoint | ||
| router.get('/v1/langchain/test', langchainController.test) | ||
|
|
||
| // Chat with system + user prompts | ||
| router.post('/v1/langchain/chat', langchainController.chat) | ||
|
|
||
| // Simple user prompt | ||
| router.post('/v1/langchain/prompt', langchainController.prompt) | ||
|
|
||
| // Custom template execution | ||
| router.post('/v1/langchain/template', langchainController.customTemplate) | ||
morningstarxcdcode marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| export { router as LangChainRouter } | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.