-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: docker compose shit + kr file storage v0
- Loading branch information
1 parent
52992ab
commit c5dabdd
Showing
6 changed files
with
161 additions
and
44 deletions.
There are no files selected for viewing
This file contains 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,26 @@ | ||
.PHONY: help docker clean | ||
|
||
.DEFAULT_GOAL := help | ||
|
||
## Colors | ||
YELLOW := $(shell tput -Txterm setaf 3) | ||
RESET := $(shell tput -Txterm sgr0) | ||
|
||
## Help documentation | ||
help: | ||
@echo "${YELLOW}Available commands:${RESET}" | ||
@echo | ||
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST) | ||
|
||
## Docker compose up | ||
docker: ## Run docker compose up | ||
docker-compose -f docker/docker-compose.yml up -d | ||
while ! docker exec -it pg pg_isready -U postgres; do sleep 1; done | ||
docker exec -it pg psql -U postgres -c "CREATE DATABASE mydatabase;" > /dev/null 2>&1 || echo "Database already exists" | ||
docker exec -i pg psql -U postgres -d mydatabase < assistants-core/src/migrations.sql > /dev/null 2>&1 || echo "Migrations already applied" | ||
|
||
## Stop and remove containers | ||
clean: ## Stop and remove docker-postgres-1, docker-redis-1, docker-minio-1 containers | ||
docker stop $$(docker ps -a -q --filter name=pg --filter name=redis --filter name=minio1) > /dev/null 2>&1 || echo "No containers to stop" | ||
docker rm $$(docker ps -a -q --filter name=pg --filter name=redis --filter name=minio1) > /dev/null 2>&1 || true | ||
docker volume rm $$(docker volume ls -q --filter name=pg --filter name=redis --filter name=minio1) > /dev/null 2>&1 || true |
This file contains 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 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 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,76 @@ | ||
|
||
|
||
|
||
use rusoto_core::Region; | ||
use rusoto_s3::{S3Client, S3}; | ||
|
||
pub struct KnowledgeRetrieval { | ||
s3_client: S3Client, | ||
} | ||
|
||
impl KnowledgeRetrieval { | ||
pub fn new() -> Self { | ||
let s3_client = S3Client::new(Region::Custom { | ||
name: "us-east-1".to_owned(), | ||
endpoint: "http://localhost:9000".to_owned(), | ||
}); | ||
|
||
Self { s3_client } | ||
} | ||
|
||
pub async fn upload_file(&self, file_path: &Path) -> Result<String, rusoto_core::RusotoError<rusoto_s3::PutObjectError>> { | ||
let mut file = File::open(file_path)?; | ||
let mut contents = Vec::new(); | ||
file.read_to_end(&mut contents)?; | ||
|
||
let req = PutObjectRequest { | ||
bucket: "my-bucket".to_owned(), | ||
key: file_path.file_name().unwrap().to_str().unwrap().to_owned(), | ||
body: Some(contents.into()), | ||
..Default::default() | ||
}; | ||
|
||
self.s3_client.put_object(req).await?; | ||
|
||
Ok(file_path.file_name().unwrap().to_str().unwrap().to_owned()) | ||
} | ||
} | ||
|
||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use std::fs::File; | ||
use std::io::Write; | ||
use std::path::PathBuf; | ||
use tempfile::tempdir; | ||
|
||
#[tokio::test] | ||
async fn test_upload_file() { | ||
// Create a temporary directory. | ||
let dir = tempdir().unwrap(); | ||
|
||
// Create a file path in the temporary directory. | ||
let file_path = dir.path().join("test.txt"); | ||
|
||
// Write some data to the file. | ||
let mut file = File::create(&file_path).unwrap(); | ||
writeln!(file, "Hello, world!").unwrap(); | ||
|
||
// Create a new KnowledgeRetrieval instance. | ||
let kr = KnowledgeRetrieval::new(); | ||
|
||
// Upload the file. | ||
let result = kr.upload_file(&file_path).await; | ||
|
||
// Check that the upload was successful. | ||
assert!(result.is_ok()); | ||
|
||
// Check that the returned key is correct. | ||
assert_eq!(result.unwrap(), "test.txt"); | ||
|
||
// Clean up the temporary directory. | ||
dir.close().unwrap(); | ||
} | ||
} | ||
|
This file contains 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 |
---|---|---|
@@ -1,3 +1,6 @@ | ||
|
||
\c mydatabase; | ||
|
||
-- Drop existing tables | ||
DROP TABLE IF EXISTS assistants; | ||
DROP TABLE IF EXISTS threads; | ||
|
This file contains 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,30 @@ | ||
version: '3.8' | ||
services: | ||
postgres: | ||
container_name: pg | ||
image: postgres | ||
restart: always | ||
environment: | ||
POSTGRES_PASSWORD: secret | ||
POSTGRES_DB: mydatabase | ||
ports: | ||
- 5432:5432 | ||
command: postgres | ||
redis: | ||
container_name: redis | ||
image: redis | ||
restart: always | ||
ports: | ||
- 6379:6379 | ||
|
||
minio: | ||
container_name: minio1 | ||
image: minio/minio | ||
restart: always | ||
ports: | ||
- 9000:9000 | ||
- 9001:9001 | ||
environment: | ||
MINIO_ROOT_USER: minioadmin | ||
MINIO_ROOT_PASSWORD: minioadmin | ||
command: server /data --console-address ":9001" |