integrating agentic chroma
This commit is contained in:
parent
9b11fea0e7
commit
e7f7a79d86
@ -0,0 +1,29 @@
|
|||||||
|
```markdown
|
||||||
|
# Creating an Ollama Blog Writer: A Hilariously Tedious Adventure
|
||||||
|
|
||||||
|
Hey tech enthusiasts! 👋 I’m back with another installment of my tech journey, but this time it’s personal. I decided to create a Python script that not only writes blogs for me (please don’t tell my boss), but also uses Ollama for some AI-assisted content creation and connects with Trilium for structured note-taking. Let’s dive into the details!
|
||||||
|
|
||||||
|
### Step 1: Get Your Ollama On
|
||||||
|
|
||||||
|
First things first, I needed a Python file that could talk to my local Ollama instance. If you haven't heard of Ollama, it's like a tiny llama in your terminal that helps with text generation. It took me a while to figure out how to configure the `.env` file and set up the connection properly. But once I did, I was off to a running start!
|
||||||
|
|
||||||
|
### Step 2: Connecting Trilium for Structured Notes
|
||||||
|
|
||||||
|
For this part, I used a Python library called `trilium-py` (because why not?). It's like having a brain that can store and retrieve information in an organized way. To make sure my notes are super structured, I had to find the right prompts and ensure they were fed into Ollama correctly. This part was more about figuring out how to structure the data than actually coding—but hey, it’s all part of the fun!
|
||||||
|
|
||||||
|
### Step 3: Automating the Blog Creation
|
||||||
|
|
||||||
|
Now that I have my notes and AI-generated content sorted, it was time to automate the blog creation process. Here’s where things got a bit Git-y (yes, I made up that word). I wrote a script that would create a new branch in our company's blog repo, push the changes, and voilà—a PR! Just like that, my humble contributions were ready for review by the big boss.
|
||||||
|
|
||||||
|
### Step 4: Sending Notifications to Matrix
|
||||||
|
|
||||||
|
Finally, as any good DevRel should do, I sent out a notification to our internal Matrix channel. It’s like Slack but with more tech talk and less memes about dogs in hats. The message was short and sweet—just a summary of the blog changes and a request for feedback. Hey, if Elon can tweet at Tesla shareholders, why not send a quick matrix message?
|
||||||
|
|
||||||
|
### Wrapping Up
|
||||||
|
|
||||||
|
Creating this Ollama Blog Writer wasn’t just about writing better blogs (though that would be nice). It was about embracing the joy of automation and the occasional struggle to get things working right. I learned a lot about Python libraries, local server configurations, and how to communicate effectively with my team via Matrix.
|
||||||
|
|
||||||
|
So there you have it—a step-by-step guide on how not to write blogs but definitely how to automate the process. If you’re into tech, automation, or just want to laugh at someone else’s coding mishaps, this blog is for you!
|
||||||
|
|
||||||
|
Keep on hacking (and automating), [Your Name]
|
||||||
|
```
|
@ -1,90 +0,0 @@
|
|||||||
Certainly! Below is the markdown representation of the Python script outline provided:
|
|
||||||
|
|
||||||
```markdown
|
|
||||||
# Ollama Blog Post Generation Script
|
|
||||||
|
|
||||||
This script automates the process of generating a new blog post by integrating Trilium notes, using Ollama for content generation, and pushing the result to a Git repository. It also sends a notification via Matrix.
|
|
||||||
|
|
||||||
## Steps Involved:
|
|
||||||
1. **Retrieve Notes from Trilium**
|
|
||||||
2. **Generate Content with Ollama**
|
|
||||||
3. **Automate Git Operations**
|
|
||||||
4. **Notify Matrix**
|
|
||||||
|
|
||||||
## Python Script Outline
|
|
||||||
|
|
||||||
```python
|
|
||||||
# Import necessary libraries
|
|
||||||
import os
|
|
||||||
from trilium_api import Client
|
|
||||||
import requests
|
|
||||||
from datetime import datetime
|
|
||||||
from git import Repo, Remote, GitCommandError, InvalidGitRepositoryError
|
|
||||||
from contextlib import contextmanager
|
|
||||||
|
|
||||||
@contextmanager
|
|
||||||
def cd(new_directory):
|
|
||||||
"""Context manager for changing the current working directory"""
|
|
||||||
previous_dir = os.getcwd()
|
|
||||||
os.chdir(new_directory)
|
|
||||||
try:
|
|
||||||
yield
|
|
||||||
finally:
|
|
||||||
os.chdir(previous_dir)
|
|
||||||
|
|
||||||
# Environment variables
|
|
||||||
TRILUM_API_URL = "http://trilum-host:8080"
|
|
||||||
OLLAMA_API_URL = "http://ollama-host:3000"
|
|
||||||
GITHUB_TOKEN = os.environ['GITHUB_TOKEN']
|
|
||||||
MATRIX_NOTIFY_URL = "http://matrix-bot/notify"
|
|
||||||
|
|
||||||
# Step 1: Retrieve notes from Trilium
|
|
||||||
client = Client()
|
|
||||||
notes = client.search("title:blog AND date_modified:>2023-10-01")
|
|
||||||
selected_notes = [note for note in notes if len(note.content) > 100]
|
|
||||||
|
|
||||||
# Step 2: Generate content with Ollama
|
|
||||||
prompt = " ".join([n.content[:50] for n in selected_notes])
|
|
||||||
ollama_content = requests.get(f"{OLLAMA_API_URL}/generate?prompt={prompt}").json()['content']
|
|
||||||
|
|
||||||
# Step 3: Git operations
|
|
||||||
repo_dir = "/path/to/blog-repo"
|
|
||||||
|
|
||||||
if not os.path.exists(repo_dir):
|
|
||||||
Repo.clone_from("ssh://user@host/repo.git", repo_dir)
|
|
||||||
|
|
||||||
with cd(repo_dir):
|
|
||||||
try:
|
|
||||||
origin = Repo().remote(name='origin')
|
|
||||||
origin.pull()
|
|
||||||
|
|
||||||
branch_name = f"new_post_{datetime.now().strftime('%Y%m%d%H%M%S')}"
|
|
||||||
Repo().create_head(branch_name, origin.refs/main)
|
|
||||||
Repo().heads[branch_name].checkout()
|
|
||||||
|
|
||||||
with open("post.md", "w") as f:
|
|
||||||
f.write(ollama_content)
|
|
||||||
|
|
||||||
origin.push(branch_name)
|
|
||||||
except GitCommandError as e:
|
|
||||||
print(f"Git error: {e}")
|
|
||||||
except InvalidGitRepositoryError:
|
|
||||||
print("The specified directory is not a git repository.")
|
|
||||||
|
|
||||||
# Step 4: Notify Matrix
|
|
||||||
requests.post(MATRIX_NOTIFY_URL, data={"text": "New blog post generated!"})
|
|
||||||
```
|
|
||||||
|
|
||||||
## Notes and Considerations:
|
|
||||||
- Ensure you have the `trilium_api` library installed. You can install it using pip if necessary.
|
|
||||||
- Set up environment variables for sensitive information like API tokens.
|
|
||||||
- Handle potential errors during Git operations and ensure proper directory setup.
|
|
||||||
- This script assumes a basic understanding of Trilium, Ollama, and Git workflows.
|
|
||||||
|
|
||||||
## Dependencies:
|
|
||||||
- `trilium_api`: For interacting with the Trilium notes application.
|
|
||||||
- `requests`: For making HTTP requests to APIs.
|
|
||||||
- `gitpython`: For interacting with Git repositories.
|
|
||||||
- Additional libraries for handling context managers (e.g., `contextlib`).
|
|
||||||
|
|
||||||
This script provides a starting point and can be further refined based on specific requirements and edge cases.
|
|
@ -111,7 +111,9 @@ class OllamaGenerator:
|
|||||||
collection_query = collection.query(query_embeddings=query_embed, n_results=100)
|
collection_query = collection.query(query_embeddings=query_embed, n_results=100)
|
||||||
print("Showing pertinent info from drafts used in final edited edition")
|
print("Showing pertinent info from drafts used in final edited edition")
|
||||||
for document in collection_query:
|
for document in collection_query:
|
||||||
print (document)
|
print (document['ids'])
|
||||||
|
print (document['embeddings'])
|
||||||
|
print (document['documents'])
|
||||||
pertinent_draft_info = '\n\n'.join(collection.query(query_embeddings=query_embed, n_results=100)['documents'][0])
|
pertinent_draft_info = '\n\n'.join(collection.query(query_embeddings=query_embed, n_results=100)['documents'][0])
|
||||||
prompt_enhanced = f"{prompt} - Generate the final document using this information from the drafts: {pertinent_draft_info} - ONLY OUTPUT THE MARKDOWN"
|
prompt_enhanced = f"{prompt} - Generate the final document using this information from the drafts: {pertinent_draft_info} - ONLY OUTPUT THE MARKDOWN"
|
||||||
print("Generating final document")
|
print("Generating final document")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user