fix: prisma + posts service
This commit is contained in:
parent
11082d93a5
commit
435b7f3fca
@ -1,7 +1,7 @@
|
||||
version: '3.8'
|
||||
services:
|
||||
postgres:
|
||||
image: postgres:latest
|
||||
image: pgvector/pgvector:pg16
|
||||
container_name: postgres
|
||||
environment:
|
||||
POSTGRES_USER: user
|
||||
|
1226
back/package-lock.json
generated
1226
back/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -20,12 +20,15 @@
|
||||
"test:e2e": "jest --config ./test/jest-e2e.json"
|
||||
},
|
||||
"dependencies": {
|
||||
"@langchain/community": "^0.2.28",
|
||||
"@langchain/ollama": "^0.0.4",
|
||||
"@nestjs/common": "^10.4.1",
|
||||
"@nestjs/core": "^10.4.1",
|
||||
"@nestjs/passport": "^10.0.3",
|
||||
"@nestjs/platform-express": "^10.4.1",
|
||||
"@prisma/client": "^5.18.0",
|
||||
"dotenv": "^16.4.5",
|
||||
"langchain": "^0.2.16",
|
||||
"multer": "^1.4.5-lts.1",
|
||||
"passport": "^0.7.0",
|
||||
"passport-http": "^0.3.0",
|
||||
|
@ -1,18 +1,20 @@
|
||||
datasource db {
|
||||
provider = "postgresql"
|
||||
url = env("DATABASE_URL")
|
||||
generator client {
|
||||
provider = "prisma-client-js"
|
||||
binaryTargets = ["native", "debian-openssl-3.0.x"]
|
||||
previewFeatures = ["postgresqlExtensions"]
|
||||
}
|
||||
|
||||
generator client {
|
||||
provider = "prisma-client-js"
|
||||
binaryTargets = ["native", "debian-openssl-3.0.x"]
|
||||
datasource db {
|
||||
provider = "postgresql"
|
||||
url = env("DATABASE_URL")
|
||||
extensions = [pgvector(map: "vector")]
|
||||
}
|
||||
|
||||
model Post {
|
||||
id Int @id @default(autoincrement())
|
||||
text String?
|
||||
media String? // URL или путь к файлу
|
||||
mediaType String? // "image", "video", "drawing", "code"
|
||||
code String? // Код, который нужно запустить
|
||||
//TODO Добавить vector для оценки близости постов
|
||||
id Int @id @default(autoincrement())
|
||||
text String?
|
||||
media String? // URL или путь к файлу
|
||||
mediaType String? // "image", "video", "drawing", "code"
|
||||
code String? // Код, который нужно запустить
|
||||
embedding Float[] //Unsupported("vector")?
|
||||
}
|
||||
|
@ -8,7 +8,7 @@ export class PostsController {
|
||||
constructor(private readonly postsService: PostsService) {}
|
||||
|
||||
@Post()
|
||||
async create(@Body() createPostDto: { text?: string; media?: string; mediaType?: string; code?: string }) {
|
||||
async create(@Body() createPostDto: { text?: string; media?: string; mediaType?: string; code?: string; embedding?: number[] }) {
|
||||
return this.postsService.create(createPostDto);
|
||||
}
|
||||
|
||||
|
@ -1,13 +1,32 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { PrismaService } from '../prisma.service';
|
||||
import { MemoryVectorStore } from 'langchain/vectorstores/memory';
|
||||
import { RecursiveCharacterTextSplitter } from 'langchain/text_splitter';
|
||||
import { OllamaEmbeddings } from '@langchain/ollama';
|
||||
|
||||
|
||||
|
||||
const embeddings = new OllamaEmbeddings({
|
||||
model: 'llama2',
|
||||
baseUrl: 'http://host.docker.internal:11434',
|
||||
});
|
||||
|
||||
@Injectable()
|
||||
export class PostsService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
async create(data: { text?: string; media?: string; mediaType?: string; code?: string }) {
|
||||
async create(data: { text?: string; media?: string; mediaType?: string; code?: string; embedding?: number[] }) {
|
||||
const splitter = RecursiveCharacterTextSplitter.fromLanguage("markdown", {
|
||||
chunkSize: 500,
|
||||
chunkOverlap: 0,
|
||||
});
|
||||
const output = await splitter.createDocuments([data.text]);
|
||||
console.log(output)
|
||||
const vectorstore = await MemoryVectorStore.fromDocuments(output, embeddings);
|
||||
console.log(vectorstore);
|
||||
data.embedding ??= vectorstore.memoryVectors[0].embedding
|
||||
return this.prisma.post.create({
|
||||
data,
|
||||
data,
|
||||
});
|
||||
}
|
||||
|
||||
|
1169
front/package-lock.json
generated
1169
front/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -7,8 +7,11 @@
|
||||
"@testing-library/react": "^13.4.0",
|
||||
"@testing-library/user-event": "^13.5.0",
|
||||
"axios": "^1.7.4",
|
||||
"markdown-it": "^14.1.0",
|
||||
"react": "^18.3.1",
|
||||
"react-dom": "^18.3.1",
|
||||
"react-markdown": "^9.0.1",
|
||||
"react-markdown-editor-lite": "^1.3.4",
|
||||
"react-scripts": "5.0.1",
|
||||
"web-vitals": "^2.1.4"
|
||||
},
|
||||
|
@ -1,12 +1,16 @@
|
||||
import React, { useState, useEffect } from 'react';
|
||||
import axios from 'axios';
|
||||
import ReactMarkdown from 'react-markdown';
|
||||
import MarkdownEditor from 'react-markdown-editor-lite';
|
||||
import 'react-markdown-editor-lite/lib/index.css';
|
||||
import MarkdownIt from 'markdown-it';
|
||||
|
||||
// Инициализация MarkdownIt
|
||||
const mdParser = new MarkdownIt();
|
||||
|
||||
const Posts = () => {
|
||||
const [posts, setPosts] = useState([]);
|
||||
const [text, setText] = useState('');
|
||||
const [media, setMedia] = useState('');
|
||||
const [mediaType, setMediaType] = useState('');
|
||||
const [code, setCode] = useState('');
|
||||
|
||||
const axiosInstance = axios.create({
|
||||
baseURL: 'http://localhost:3000',
|
||||
@ -31,45 +35,28 @@ const Posts = () => {
|
||||
const handleSubmit = async (e) => {
|
||||
e.preventDefault();
|
||||
try {
|
||||
const newPost = { text, media, mediaType, code };
|
||||
const newPost = { text };
|
||||
await axiosInstance.post('/posts', newPost);
|
||||
fetchPosts(); // Обновляем список постов после создания нового
|
||||
setText('');
|
||||
setMedia('');
|
||||
setMediaType('');
|
||||
setCode('');
|
||||
} catch (error) {
|
||||
console.error('Error creating post:', error);
|
||||
}
|
||||
};
|
||||
|
||||
const handleEditorChange = ({ text }) => {
|
||||
setText(text);
|
||||
};
|
||||
|
||||
return (
|
||||
<div>
|
||||
<h1>Posts</h1>
|
||||
<form onSubmit={handleSubmit}>
|
||||
<input
|
||||
type="text"
|
||||
placeholder="Text"
|
||||
<MarkdownEditor
|
||||
value={text}
|
||||
onChange={(e) => setText(e.target.value)}
|
||||
/>
|
||||
<input
|
||||
type="text"
|
||||
placeholder="Media URL"
|
||||
value={media}
|
||||
onChange={(e) => setMedia(e.target.value)}
|
||||
/>
|
||||
<input
|
||||
type="text"
|
||||
placeholder="Media Type"
|
||||
value={mediaType}
|
||||
onChange={(e) => setMediaType(e.target.value)}
|
||||
/>
|
||||
<input
|
||||
type="text"
|
||||
placeholder="Code"
|
||||
value={code}
|
||||
onChange={(e) => setCode(e.target.value)}
|
||||
style={{ height: '300px' }}
|
||||
renderHTML={(text) => mdParser.render(text)}
|
||||
onChange={handleEditorChange}
|
||||
/>
|
||||
<button type="submit">Create Post</button>
|
||||
</form>
|
||||
@ -77,10 +64,7 @@ const Posts = () => {
|
||||
<ul>
|
||||
{posts.map((post, index) => (
|
||||
<li key={index}>
|
||||
<p>{post.text}</p>
|
||||
{post.media && <img src={post.media} alt="Post media" />}
|
||||
<p>Media Type: {post.mediaType}</p>
|
||||
<p>Code: {post.code}</p>
|
||||
<ReactMarkdown>{post.text}</ReactMarkdown>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
@ -88,4 +72,4 @@ const Posts = () => {
|
||||
);
|
||||
};
|
||||
|
||||
export default Posts;
|
||||
export default Posts;
|
Loading…
Reference in New Issue
Block a user