Skip to content

Commit

Permalink
Release: 0.5.1-alpha1
Browse files Browse the repository at this point in the history
  • Loading branch information
Estrada Irribarra, Rodrigo Andres committed Oct 20, 2024
1 parent 32a25e6 commit 853fc01
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 74 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ Welcome to [**StoryCraftr**](https://storycraftr.app), the open-source project d

You can find the release notes for version `v0.5.0-alpha1` [here](https://github.com/raestrada/storycraftr/releases/tag/v0.5.0-alpha1).

## Installation
## Step 1: Install StoryCraftr

You can install the current version of **StoryCraftr** via `pipx` using the following command:
First, install **StoryCraftr** using [pipx](https://pypa.github.io/pipx/), a tool to help you install and run Python applications in isolated environments. It works on most platforms, including macOS, Linux, and Windows. Using `pipx` ensures that **StoryCraftr** runs in its own virtual environment, keeping your system's Python installation clean.

To install **StoryCraftr**, run the following command:

```bash
pipx install git+https://github.com/raestrada/[email protected].0-alpha1
pipx install git+https://github.com/raestrada/[email protected].1-alpha1
```

### Important: Before using StoryCraftr, make sure to set your OpenAI API key:
Expand Down
2 changes: 1 addition & 1 deletion docs/getting_started.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ First, install **StoryCraftr** using [pipx](https://pypa.github.io/pipx/), a too
To install **StoryCraftr**, run the following command:

```bash
pipx install git+https://github.com/raestrada/[email protected].0-alpha1
pipx install git+https://github.com/raestrada/[email protected].1-alpha1
```

### Important: Before running the `storycraftr` command
Expand Down
2 changes: 1 addition & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ <h1>Welcome to StoryCraftr 📚✨</h1>
<div class="coming-soon">
🚧 Coming Soon - Beta Release! 🚀
<br>Meanwhile, you can download the Alpha version, which is fully usable but may still have bugs:
<code>pipx install git+https://github.com/raestrada/[email protected].0-alpha1</code>
<code>pipx install git+https://github.com/raestrada/[email protected].1-alpha1</code>
</div>
</section>

Expand Down
155 changes: 86 additions & 69 deletions storycraftr/agent/agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,67 @@
console = Console()


def get_vector_store_id_by_name(assistant_name):
"""Retrieve the vector store ID by name."""
vector_stores = client.beta.vector_stores.list()

# Construir el nombre predecible del vector store
expected_name = f"{assistant_name} Docs"

# Buscar el vector store cuyo nombre coincida con el esperado
for vector_store in vector_stores.data:
if vector_store.name == expected_name:
return vector_store.id

console.print(
f"[bold red]No vector store found with name '{expected_name}'.[/bold red]"
)
return None


def upload_markdown_files_to_vector_store(
vector_store_id, book_path, progress=None, task=None
):
"""
Function to upload all markdown files from the book path to the specified vector store.
Parameters:
vector_store_id (str): ID of the vector store to which files will be uploaded.
book_path (str): Path to the book directory containing markdown files.
progress (Progress, optional): Progress bar object for tracking progress.
task (Task, optional): Task ID for progress tracking.
"""
console.print(
f"[bold blue]Uploading all knowledge files from '{book_path}'...[/bold blue]"
)

# Cargar los archivos markdown
md_files = load_markdown_files(book_path)

if len(md_files) == 0:
console.print("[bold yellow]No Markdown files found to upload.[/bold yellow]")
return

file_streams = [open(file_path, "rb") for file_path in md_files]

# Subir los archivos al vector store
file_batch = client.beta.vector_stores.file_batches.upload_and_poll(
vector_store_id=vector_store_id, files=file_streams
)

# Monitorear el progreso
while file_batch.status == "queued" or file_batch.status == "in_progress":
if progress and task:
progress.update(task, description=f"{file_batch.status}...")
else:
console.print(f"[bold yellow]{file_batch.status}...[/bold yellow]")
time.sleep(1)

console.print(
f"[bold green]Files uploaded successfully to vector store '{vector_store_id}'.[/bold green]"
)


def load_markdown_files(book_path):
"""Load all Markdown files from the book's directory and subdirectories."""
console.print(
Expand Down Expand Up @@ -60,7 +121,6 @@ def delete_assistant(book_path):
def create_or_get_assistant(book_path, progress: Progress = None, task=None):
name = book_path.split("/")[-1]

# Progress message for searching an existing assistant
if progress and task:
progress.update(
task, description=f"Searching for existing assistant '{name}'..."
Expand All @@ -75,7 +135,6 @@ def create_or_get_assistant(book_path, progress: Progress = None, task=None):

for assistant in assistants.data:
if assistant.name == name:
# Progress message for found assistant
if progress and task:
progress.update(task, description=f"Assistant {name} already exists.")
else:
Expand All @@ -84,84 +143,30 @@ def create_or_get_assistant(book_path, progress: Progress = None, task=None):
)
return assistant

# Step 1: Create a vector store for the book
if progress and task:
progress.update(task, description=f"Creating vector store for '{book_path}'...")
else:
console.print(
f"[bold blue]Creating vector store for '{book_path}'...[/bold blue]"
)

# Crear vector store
vector_store = client.beta.vector_stores.create(name=f"{name} Docs")

# Step 2: Upload Knowledge (Markdown files)
if progress and task:
progress.update(task, description=f"Uploading knowledge from '{book_path}'...")
else:
console.print(
f"[bold blue]Uploading knowledge from '{book_path}'...[/bold blue]"
)

md_files = load_markdown_files(book_path)

if len(md_files):
file_streams = [open(file_path, "rb") for file_path in md_files]

file_batch = client.beta.vector_stores.file_batches.upload_and_poll(
vector_store_id=vector_store.id, files=file_streams
)

while file_batch.status == "queued" or file_batch.status == "in_progress":
if progress and task:
progress.update(task, description=f"{file_batch.status}...")
else:
console.print(f"[bold yellow]{file_batch.status}...[/bold yellow]")
time.sleep(1)

# Step 3: Create the Assistant
if progress and task:
progress.update(task, description=f"Reading behavior instructions...")
else:
console.print(f"[bold blue]Reading behavior instructions...[/bold blue]")
# Subir archivos markdown al vector store usando la función común
upload_markdown_files_to_vector_store(vector_store.id, book_path, progress, task)

# Crear el asistente
with open("behaviors/default.txt", "r") as file:
instructions = file.read()

if progress and task:
progress.update(task, description=f"Creating assistant '{name}'...")
else:
console.print(f"[bold blue]Creating assistant '{name}'...[/bold blue]")

assistant = client.beta.assistants.create(
instructions=instructions,
name=name,
tools=[{"type": "code_interpreter"}, {"type": "file_search"}],
model="gpt-4o",
)

# Step 4: Associate the assistant with the vector store
if progress and task:
progress.update(
task, description=f"Associating assistant '{name}' with vector store..."
)
else:
console.print(
f"[bold blue]Associating assistant '{name}' with vector store...[/bold blue]"
)

if len(md_files):
assistant = client.beta.assistants.update(
assistant_id=assistant.id,
tool_resources={"file_search": {"vector_store_ids": [vector_store.id]}},
)
# Asociar el asistente con el vector store
assistant = client.beta.assistants.update(
assistant_id=assistant.id,
tool_resources={"file_search": {"vector_store_ids": [vector_store.id]}},
)

# Success message
if progress and task:
progress.update(task, description=f"Assistant '{name}' created successfully.")
else:
console.print(
f"[bold green]Assistant '{name}' created successfully.[/bold green]"
)
console.print(f"[bold green]Assistant '{name}' created successfully.[/bold green]")

return assistant

Expand Down Expand Up @@ -322,9 +327,21 @@ def get_thread():

# Function to update the assistant's knowledge with new files
def update_agent_files(book_path, assistant):
delete_assistant(book_path)
create_or_get_assistant(book_path)
# Obtener el nombre del asistente
assistant_name = assistant.name

# Obtener el vector_store_id por nombre
vector_store_id = get_vector_store_id_by_name(assistant_name)

if not vector_store_id:
console.print(
f"[bold red]Error: Could not find vector store for assistant '{assistant_name}'.[/bold red]"
)
return

# Usar la función común para subir los archivos al vector store correspondiente
upload_markdown_files_to_vector_store(vector_store_id, book_path)

console.print(
f"[bold green]Files updated successfully in assistant '{assistant.name}'.[/bold green]"
) # Success message
)

0 comments on commit 853fc01

Please sign in to comment.