Use Ollama to communicate with a model, for example, Llama 3.2 3B.
We want the model to perform actions on the contents of files.
Allow iteration over a set of txt files in a folder. The contents of these files are what will be used to communicate with the model.
Add a sentence to the end of the file content, ensuring its length within the model's context limit.
We want the model to structure very long documents by reducing the information following a pre-established structure. What type of prompting is best: zero-shot, few-shot, chaining, tree of thoughts, etc.?
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Certainly! Below is a Python code that accomplishes the tasks you've described. We'll use the ollama library to interact with the model, iterate over .txt files in a folder, append a sentence to the file content, and use few-shot prompting to structure very long documents.
First, ensure you have the ollama library installed. If not, you can install it using:
bash
pip install ollama
Here's the complete Python code:
python
import os
from ollama import Client
# Initialize the Ollama client
client = Client(host='http://localhost:11434')
# Folder containing the .txt files
folder_path = 'path_to_your_folder'
# Sentence to append to the file content
append_sentence = "This is an appended sentence."
# Model's context limit (example value, adjust according to your model)
context_limit = 2000 # Example context limit, adjust as needed
def append_to_content(content, append_sentence, context_limit):
"""Append a sentence to the content if it fits within the context limit."""
if len(content) + len(append_sentence) <= context_limit:
return content + " " + append_sentence
else:
return content
def structure_document(content):
"""Use few-shot prompting to structure the document."""
few_shot_prompt = (
"Here are some examples of how to structure a document:\n"
"Example 1:\n"
"Original: The quick brown fox jumps over the lazy dog.\n"
"Structured: The quick brown fox -> jumps over -> the lazy dog.\n\n"
"Example 2:\n"
"Original: She sells seashells by the seashore.\n"
"Structured: She sells -> seashells -> by the seashore.\n\n"
"Now, structure the following document:\n"
f"{content}"
)
response = client.generate(model='llama3', prompt=few_shot_prompt)
return response['response'].strip()
def process_files(folder_path):
"""Process each .txt file in the folder."""
for filename in os.listdir(folder_path):
if filename.endswith(".txt"):
file_path = os.path.join(folder_path, filename)
with open(file_path, 'r') as file:
content = file.read()
# Append sentence if it fits within the context limit
content = append_to_content(content, append_sentence, context_limit)
# Structure the document using few-shot prompting
structured_content = structure_document(content)
# Save the structured content back to the file
with open(file_path, 'w') as file:
file.write(structured_content)
print(f"Processed and saved: {filename}")
# Process all .txt files in the folder
process_files(folder_path)
Explanation:
Ollama Client Initialization: We initialize the Ollama client to communicate with the model.
Folder Path and Sentence: We specify the folder containing the .txt files and the sentence to append.
Context Limit: We set a context limit for the model (adjust this based on your model's actual limit).
Append to Content: The append_to_content function appends the sentence to the content if it fits within the context limit.
Structure Document: The structure_document function uses few-shot prompting to structure the document. It provides examples of how to structure text and then asks the model to structure the given content.
Process Files: The process_files function iterates over all .txt files in the specified folder, reads their content, appends the sentence if possible, structures the content, and saves the changes back to the file.
Prompting Strategy:
Few-shot prompting is used here because it provides the model with examples of the desired output format, which helps the model understand and generate structured content more effectively.