-
Notifications
You must be signed in to change notification settings - Fork 235
Adds the ability to pass files to ramalama run
#1570
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
Merged
Merged
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,6 @@ version = "0.9.3" | |
description = "RamaLama is a command line tool for working with AI LLM models." | ||
readme = "README.md" | ||
requires-python = ">=3.10" | ||
license = { file = "LICENSE" } | ||
keywords = ["ramalama", "llama", "AI"] | ||
dependencies = [ | ||
"argcomplete", | ||
|
@@ -18,6 +17,8 @@ maintainers = [ | |
{ name="Eric Curtin", email = "[email protected]" }, | ||
] | ||
|
||
[project.license] | ||
text = "MIT" | ||
|
||
[project.optional-dependencies] | ||
dev = [ | ||
|
@@ -56,6 +57,7 @@ ramalama = "ramalama.cli:main" | |
|
||
[tool.setuptools] | ||
include-package-data = true | ||
license-files = ["LICENSE"] | ||
|
||
[tool.black] | ||
line-length = 120 | ||
|
@@ -93,7 +95,7 @@ log_cli_date_format = "%Y-%m-%d %H:%M:%S" | |
|
||
|
||
[tool.setuptools.packages.find] | ||
include = ["ramalama"] | ||
include = ["ramalama", "ramalama.*"] | ||
|
||
[tool.setuptools.data-files] | ||
"share/ramalama" = ["shortnames/shortnames.conf"] | ||
|
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
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,3 @@ | ||
from ramalama.file_upload import file_loader, file_types | ||
|
||
__all__ = ["file_loader", "file_types"] |
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,57 @@ | ||
import os | ||
from string import Template | ||
from warnings import warn | ||
|
||
from ramalama.file_upload.file_types import base, txt | ||
|
||
SUPPORTED_EXTENSIONS = { | ||
'.txt': txt.TXTFileUpload, | ||
'.sh': txt.TXTFileUpload, | ||
'.md': txt.TXTFileUpload, | ||
'.yaml': txt.TXTFileUpload, | ||
'.yml': txt.TXTFileUpload, | ||
'.json': txt.TXTFileUpload, | ||
'.csv': txt.TXTFileUpload, | ||
'.toml': txt.TXTFileUpload, | ||
} | ||
|
||
|
||
class BaseFileUploader: | ||
""" | ||
Base class for file upload handlers. | ||
This class should be extended by specific file type handlers. | ||
""" | ||
|
||
def __init__(self, files: list[base.BaseFileUpload], delim_string: str = "<!--start_document $name-->"): | ||
self.files = files | ||
self.document_delimiter: Template = Template(delim_string) | ||
|
||
def load(self) -> str: | ||
""" | ||
Generate the output string by concatenating the processed files. | ||
""" | ||
output = (f"\n{self.document_delimiter.substitute(name=f.file)}\n{f.load()}" for f in self.files) | ||
return "".join(output) | ||
|
||
|
||
class FileUpLoader(BaseFileUploader): | ||
def __init__(self, file_path: str): | ||
if not os.path.exists(file_path): | ||
raise ValueError(f"{file_path} does not exist.") | ||
|
||
if not os.path.isdir(file_path): | ||
files = [file_path] | ||
else: | ||
files = [os.path.join(root, name) for root, _, files in os.walk(file_path) for name in files] | ||
|
||
extensions = [os.path.splitext(f)[1].lower() for f in files] | ||
|
||
if set(extensions) - set(SUPPORTED_EXTENSIONS): | ||
warning_message = ( | ||
f"Unsupported file types found: {set(extensions) - set(SUPPORTED_EXTENSIONS)}\n" | ||
f"Supported types are: {set(SUPPORTED_EXTENSIONS.keys())}" | ||
) | ||
warn(warning_message) | ||
|
||
files = [SUPPORTED_EXTENSIONS[ext](file=f) for ext, f in zip(extensions, files) if ext in SUPPORTED_EXTENSIONS] | ||
super().__init__(files=files) |
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,3 @@ | ||
from ramalama.file_upload.file_types import base, txt | ||
|
||
__all__ = ["base", "txt"] |
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,19 @@ | ||
from abc import ABC, abstractmethod | ||
|
||
|
||
class BaseFileUpload(ABC): | ||
""" | ||
Base class for file upload handlers. | ||
This class should be extended by specific file type handlers. | ||
""" | ||
|
||
def __init__(self, file): | ||
self.file = file | ||
|
||
@abstractmethod | ||
def load(self) -> str: | ||
""" | ||
Load the content of the file. | ||
This method should be implemented by subclasses to handle specific file types. | ||
""" | ||
pass |
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,10 @@ | ||
from ramalama.file_upload.file_types.base import BaseFileUpload | ||
|
||
|
||
class PDFFileUpload(BaseFileUpload): | ||
def load(self) -> str: | ||
""" | ||
Load the content of the PDF file. | ||
This method should be implemented to handle PDF file reading. | ||
""" | ||
return "" | ||
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,12 @@ | ||||||
from ramalama.file_upload.file_types.base import BaseFileUpload | ||||||
|
||||||
|
||||||
class TXTFileUpload(BaseFileUpload): | ||||||
def load(self) -> str: | ||||||
""" | ||||||
Load the content of the text file. | ||||||
""" | ||||||
|
||||||
# TODO: Support for non-default encodings? | ||||||
with open(self.file, 'r') as f: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Suggested change
|
||||||
return f.read() |
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,6 @@ | ||
# # tests/conftest.py | ||
# import pytest | ||
|
||
# @pytest.fixture(autouse=True) | ||
# def set_container_engine_env(monkeypatch): | ||
# monkeypatch.setenv("RAMALAMA_CONTAINER_ENGINE", "docker") |
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,6 @@ | ||
name,age,city,occupation | ||
John Doe,30,New York,Engineer | ||
Jane Smith,25,San Francisco,Designer | ||
Bob Johnson,35,Chicago,Manager | ||
Alice Brown,28,Boston,Developer | ||
Charlie Wilson,32,Seattle,Analyst |
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,20 @@ | ||
{ | ||
"name": "test_data", | ||
"version": "1.0.0", | ||
"description": "Sample JSON data for testing file upload functionality", | ||
"features": [ | ||
"text_processing", | ||
"file_upload", | ||
"chat_integration" | ||
], | ||
"metadata": { | ||
"author": "Test User", | ||
"created": "2024-01-01", | ||
"tags": ["test", "sample", "json"] | ||
}, | ||
"config": { | ||
"enabled": true, | ||
"max_file_size": 1048576, | ||
"supported_formats": [".txt", ".md", ".json", ".yaml", ".yml", ".csv", ".toml", ".pdf"] | ||
} | ||
} |
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,20 @@ | ||
# Sample Markdown File | ||
|
||
This is a sample markdown file for testing the file upload functionality. | ||
|
||
## Features | ||
|
||
- **Bold text** and *italic text* | ||
- Lists with bullets | ||
- Code blocks: `inline code` | ||
- Links: [Example](https://example.com) | ||
|
||
## Code Example | ||
|
||
```python | ||
def hello_world(): | ||
print("Hello, World!") | ||
return "Success" | ||
``` | ||
|
||
This file should be processed by the TXTFileUpload class since markdown is treated as text. |
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,36 @@ | ||
#!/bin/bash | ||
|
||
# Sample shell script for testing file upload functionality | ||
|
||
echo "Hello, World! This is a test script." | ||
|
||
# Function to demonstrate script functionality | ||
test_function() { | ||
local message="$1" | ||
echo "Testing: $message" | ||
return 0 | ||
} | ||
|
||
# Variables | ||
NAME="Test Script" | ||
VERSION="1.0.0" | ||
|
||
# Main execution | ||
echo "Running $NAME version $VERSION" | ||
|
||
# Test the function | ||
test_function "file upload functionality" | ||
|
||
# Conditional logic | ||
if [ -f "test_file.txt" ]; then | ||
echo "Test file exists" | ||
else | ||
echo "Test file does not exist" | ||
fi | ||
|
||
# Loop example | ||
for i in {1..3}; do | ||
echo "Iteration $i" | ||
done | ||
|
||
echo "Script completed successfully!" |
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,23 @@ | ||
# Sample TOML configuration file | ||
name = "test_config" | ||
version = "1.0.0" | ||
description = "Sample TOML data for testing file upload functionality" | ||
|
||
[metadata] | ||
author = "Test User" | ||
created = "2024-01-01" | ||
tags = ["test", "sample", "toml"] | ||
|
||
[config] | ||
enabled = true | ||
max_file_size = 1048576 | ||
supported_formats = [".txt", ".md", ".json", ".yaml", ".yml", ".csv", ".toml", ".pdf"] | ||
|
||
[features] | ||
text_processing = true | ||
file_upload = true | ||
chat_integration = true | ||
toml_support = true | ||
|
||
[nested.structure] | ||
with_deep_nesting = true |
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,9 @@ | ||
This is a sample text file for testing the file upload functionality. | ||
|
||
It contains multiple lines of text to test how the system handles: | ||
- Plain text content | ||
- Multiple lines | ||
- Special characters like: !@#$%^&*() | ||
- Numbers: 1234567890 | ||
|
||
This file should be processed by the TXTFileUpload class. |
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,37 @@ | ||
# Sample YAML configuration file | ||
name: test_config | ||
version: 1.0.0 | ||
description: Sample YAML data for testing file upload functionality | ||
|
||
features: | ||
- text_processing | ||
- file_upload | ||
- chat_integration | ||
- yaml_support | ||
|
||
metadata: | ||
author: Test User | ||
created: 2024-01-01 | ||
tags: | ||
- test | ||
- sample | ||
- yaml | ||
|
||
config: | ||
enabled: true | ||
max_file_size: 1048576 | ||
supported_formats: | ||
- .txt | ||
- .md | ||
- .json | ||
- .yaml | ||
- .yml | ||
- .csv | ||
- .toml | ||
|
||
nested: | ||
structure: | ||
with: | ||
deep: | ||
nesting: true |
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.
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.