Complete LangChain Ollama Integration: Building Graph-Based Multi-Persona Conversations with Local LLMs and CLI/GUI Interfaces
Comprehensive guide to integrating LangChain with Ollama for local LLM usage, featuring graph-based conversation orchestration, persona-driven responses, CLI and GUI interfaces, and iterative multi-round conversations.
Daniel Kliewer
Author, Sovereign AI


High-Level Architecture for the LangChain Application using Ollama:
The application leverages a graph structure to manage and orchestrate interactions with a Language Model (LLM) using LangChain and Ollama. The key components and their interactions are:
-
Graph Manager:
- Purpose: Manages a directed graph where each node represents an LLM prompt and its corresponding response.
- Implementation: Utilizes a graph data structure (e.g., from the
networkxlibrary) to model nodes (prompts and responses) and edges (data flow between prompts).
-
Persona Manager:
- Purpose: Handles different personas, each providing unique perspectives or areas of knowledge.
- Implementation: Defines personas as configurations or templates that tailor prompts to reflect specific viewpoints.
-
Context Manager:
- Purpose: Manages the context passed between LLM calls, ensuring each prompt is aware of relevant previous interactions.
- Implementation: Accumulates and updates context based on the graph's edges, feeding necessary information to subsequent prompts.
-
LLM Interface (via LangChain and Ollama):
- Purpose: Facilitates interactions with the LLM, generating responses to prompts with the given context and persona.
- Implementation: Uses LangChain's
LLMChainandPromptTemplate, with theOllamaLLM wrapper to construct and execute prompts.
-
Markdown Logger:
- Purpose: Records all prompts, responses, and analyses in a structured markdown file for tracking and reviewing.
- Implementation: Appends entries to a markdown file, formatting the content for readability and organization.
-
Analysis Module:
- Purpose: Analyzes previous prompts and responses, potentially generating new insights or directing the flow of the conversation.
- Implementation: Creates specialized nodes in the graph that process and reflect on prior interactions.
Implementing the Application with Ollama:
Below is a step-by-step guide to building the application using Ollama, including code snippets and explanations.
1. Set Up the Environment
Install the Necessary Python Libraries:
Ensure you have Python installed (preferably 3.7 or higher), and then install the required packages:
bash1pip install langchain networkx markdown
Install Ollama:
Ollama is a tool for running language models locally. Follow the installation instructions for your operating system:
-
macOS:
bash1brew install ollama/tap/ollama -
Linux and Windows:
Visit the Ollama GitHub repository for installation instructions specific to your platform.
Download a Model for Ollama:
Ollama can run various models. For this application, we'll use llama2 or any compatible model.
bash1ollama pull llama2
2. Import Required Modules
python1import os2import networkx as nx3from langchain import PromptTemplate, LLMChain4from langchain.llms import Ollama
3. Define the Node Class
Create a class to encapsulate the properties of each node in the graph:
python1class Node:2 def __init__(self, node_id, prompt_text, persona):3 self.id = node_id4 self.prompt_text = prompt_text5 self.response_text = None6 self.context = ""7 self.persona = persona
4. Initialize the Graph
Initialize a directed graph using networkx:
python1G = nx.DiGraph()
5. Define Personas
Create a dictionary to hold different personas and their corresponding system prompts:
python1personas = {2 "Historian": "You are a knowledgeable historian specializing in the industrial revolution.",3 "Scientist": "You are a scientist with expertise in technological advancements.",4 "Philosopher": "You are a philosopher pondering the societal impacts.",5 "Analyst": "You analyze information critically to provide insights.",6 # Add additional personas as needed7}
6. Implement the Graph Manager
Add nodes and edges to construct the conversation flow:
python1# Create initial prompt nodes with different personas2node1 = Node(1, prompt_text="Discuss the impacts of the industrial revolution.", persona="Historian")3G.add_node(node1.id, data=node1)45node2 = Node(2, prompt_text="Discuss the technological advancements during the industrial revolution.", persona="Scientist")6G.add_node(node2.id, data=node2)78# Add edges if node2 should consider node1's context9G.add_edge(node1.id, node2.id)1011# Add an analysis node12node3 = Node(3, prompt_text="", persona="Analyst")13G.add_node(node3.id, data=node3)14G.add_edge(node1.id, node3.id)15G.add_edge(node2.id, node3.id)
7. Implement the Context Manager
Define a function to collect context from predecessor nodes:
python1def collect_context(node_id):2 predecessors = list(G.predecessors(node_id))3 context = ""4 for pred_id in predecessors:5 pred_node = G.nodes[pred_id]['data']6 if pred_node.response_text:7 context += f"From {pred_node.persona}:\n{pred_node.response_text}\n\n"8 return context
8. Implement the LLM Interface with Ollama
Create a function to generate responses using LangChain and Ollama:
python1def generate_response(node):2 system_prompt = personas[node.persona]3 # Build the complete prompt4 prompt_template = PromptTemplate(5 input_variables=["system_prompt", "context", "prompt"],6 template="{system_prompt}\n\n{context}\n\n{prompt}"7 )8 # Instantiate the Ollama LLM9 llm = Ollama(10 base_url="http://localhost:11434", # Default Ollama server URL11 model="llama2", # or specify the model you have downloaded12 )13 chain = LLMChain(llm=llm, prompt=prompt_template)14 response = chain.run(15 system_prompt=system_prompt,16 context=node.context,17 prompt=node.prompt_text18 )19 return response
Note: Ensure that the Ollama server is running before executing the script:
bash1ollama serve
9. Implement the Markdown Logger
Define a function to log interactions to a markdown file:
python1def update_markdown(node):2 with open("conversation.md", "a", encoding="utf-8") as f:3 f.write(f"## Node {node.id}: {node.persona}\n\n")4 f.write(f"**Prompt:**\n\n{node.prompt_text}\n\n")5 f.write(f"**Response:**\n\n{node.response_text}\n\n---\n\n")
10. Implement the Analysis Module
Create a function for nodes that perform analysis:
python1def analyze_responses(node):2 # Collect responses from predecessor nodes3 predecessors = list(G.predecessors(node.id))4 analysis_input = ""5 for pred_id in predecessors:6 pred_node = G.nodes[pred_id]['data']7 analysis_input += f"{pred_node.persona}'s response:\n{pred_node.response_text}\n\n"89 node.prompt_text = f"Provide an analysis comparing the following perspectives:\n\n{analysis_input}"10 node.context = "" # Analysis can be based solely on the provided responses11 node.response_text = generate_response(node)12 update_markdown(node)
11. Process the Nodes
Iterate over the graph to process each node:
python1for node_id in nx.topological_sort(G):2 node = G.nodes[node_id]['data']3 if node.persona != "Analyst":4 node.context = collect_context(node_id)5 node.response_text = generate_response(node)6 update_markdown(node)7 else:8 analyze_responses(node)
Detailed Explanation:
-
Graph Processing Order:
- Use
nx.topological_sort(G)to process nodes in an order that respects dependencies, ensuring predecessor nodes are processed before successors.
- Use
-
Context Collection:
- For each node, the
collect_contextfunction gathers responses from predecessor nodes, forming the context that will be included in the prompt.
- For each node, the
-
Persona-Specific Prompts:
- The
system_promptvariable injects persona characteristics into the prompt via LangChain's templating, guiding the LLM to respond from that perspective.
- The
-
Response Generation with Ollama:
- The
generate_responsefunction constructs the prompt using thePromptTemplateand retrieves the LLM's response using LangChain'sLLMChainwith theOllamaLLM.
- The
-
Logging Interactions:
- The
update_markdownfunction appends each interaction to theconversation.mdfile, using markdown formatting for clarity and organization.
- The
-
Analysis Nodes:
- Nodes with the persona "Analyst" execute the
analyze_responsesfunction, which compiles predecessor responses and generates an analytical output.
- Nodes with the persona "Analyst" execute the
Example Output in Markdown:
The conversation.md file will contain formatted entries like:
text1## Node 1: Historian23**Prompt:**45Discuss the impacts of the industrial revolution.67**Response:**89[Historian's response...]1011---1213## Node 2: Scientist1415**Prompt:**1617Discuss the technological advancements during the industrial revolution.1819**Response:**2021[Scientist's response...]2223---2425## Node 3: Analyst2627**Prompt:**2829Provide an analysis comparing the following perspectives:30...3132**Response:**3334[Analyst's comparative analysis...]3536---
12. Expanding the Application
To enhance the application further:
-
Dynamic Node Creation:
- Based on responses, new nodes can be added to explore emerging topics.
-
Advanced Personas:
- Enrich personas with more detailed backgrounds or expertise.
-
User Interaction:
- Introduce mechanisms for user input to guide the conversation.
-
Visualization:
- Generate visual representations of the graph to illustrate the conversation flow.
13. Considerations and Best Practices
-
Ollama Model Selection:
- Ensure that the model used with Ollama is appropriate for the application's needs. Some models may require specific handling or have different capabilities.
-
Context Window Limitations:
- Be mindful of the token limit for the LLM's context window; if necessary, truncate or summarize context.
-
Error Handling:
- Implement robust error handling around LLM calls and file operations to handle exceptions gracefully.
-
Concurrency:
- For large graphs, consider asynchronous processing where dependencies allow.
-
Configuration Management:
- Use configuration files or environment variables to manage settings like the Ollama server URL and model name.
-
Privacy and Security:
- Ensure sensitive information is not exposed, especially when logging prompts and responses.
Summary:
By integrating these components with Ollama, the application can:
-
Orchestrate LLM Calls via a Graph:
- Manage complex conversational flows where prompts and responses are interconnected in non-linear ways.
-
Update Context Dynamically:
- Pass information between nodes, ensuring that each prompt is informed by relevant preceding interactions.
-
Utilize Multiple Personas:
- Simulate different perspectives by tailoring prompts to various personas, enriching the conversation.
-
Track Interaction History:
- Maintain a comprehensive record of the conversation, including analyses, in a markdown file for transparency and review.
-
Analyze and Reflect:
- Incorporate analysis steps that synthesize previous responses, potentially guiding future prompts.
Implementation Steps Recap:
-
Set Up Environment and Libraries:
- Install
langchain,networkx,markdown, and set up Ollama.
- Install
-
Define Data Structures (Nodes and Edges):
- Create the
Nodeclass to represent each point in the conversation.
- Create the
-
Initialize the Directed Graph:
- Use
networkxto manage the flow of conversations.
- Use
-
Define Personas and Their Prompts:
- Establish different perspectives through personas.
-
Build the Graph Manager Functions:
- Construct the conversation flow by adding nodes and edges.
-
Implement Context Collection Mechanism:
- Gather context from predecessor nodes for each prompt.
-
Create the LLM Interface with Ollama:
- Use LangChain's
Ollamaintegration to interface with the local LLM.
- Use LangChain's
-
Set Up the Markdown Logger:
- Record the prompts and responses in a markdown file.
-
Develop the Analysis Module:
- Analyze previous responses to generate insights.
-
Process Nodes in Topological Order:
- Execute the conversation flow respecting dependencies.
By following this architecture and implementation plan with Ollama, you can create a robust application that leverages the power of LangChain and local LLMs to generate rich, context-aware conversations from multiple perspectives, all while maintaining a clear and organized record of the interaction history.
Next Steps:
-
Testing:
- Run the application with sample prompts and personas to verify functionality.
-
Refinement:
- Adjust personas, context management, and logging based on observed outcomes.
-
Scaling:
- Expand the graph to include more nodes and complex interactions, testing the application's scalability.
-
Documentation:
- Document the code thoroughly, explaining how each component works for future maintenance and updates.
-
Model Optimization:
- Experiment with different models available in Ollama to find the best fit for your application.
By iteratively refining the application, you can tailor it to specific use cases, such as educational tools, collaborative brainstorming platforms, or complex simulation environments, all powered locally using Ollama.
Additional Resources:
-
Ollama Documentation:
- Visit the Ollama GitHub Repository for more details on installation, models, and usage.
-
LangChain Documentation:
- Explore the LangChain Documentation for in-depth guides on using LLMs, prompt templates, and chains.
-
Community Support:
- Engage with the communities around LangChain and Ollama for support, updates, and shared experiences.
Troubleshooting Tips:
-
Ollama Server Not Running:
- If you encounter connection errors, ensure the Ollama server is running with
ollama serve.
- If you encounter connection errors, ensure the Ollama server is running with
-
Model Not Found:
- Verify that the model specified in the
OllamaLLM instantiation is correctly downloaded and available.
- Verify that the model specified in the
-
Performance Issues:
- Running large models locally may require significant computational resources. Ensure your hardware meets the requirements.
-
Compatibility:
- Ensure all libraries are up-to-date to avoid compatibility issues. Use virtual environments to manage dependencies.
layout: post title: LangChain Ollama date: 2024-12-19 07:42:44 -0500 categories: ["AI Integration & Development", "Technical Tutorials"] tags: ["LangChain", "Ollama", "AI", "Python", "LLM", "Graph", "Interactive"]
1. Create the Project Directory and File Structure
Open your terminal and run the following commands to set up the project directory and files:
bash1# Create the project directory and navigate into it2mkdir langchain_graph_app3cd langchain_graph_app45# Create the main Python script6touch main.py78# Create a requirements.txt file to list dependencies9touch requirements.txt
2. Write the Code for the Files
I'll provide the code for each file. Please copy and paste the code into the respective files.
File: requirements.txt
First, specify the dependencies in a requirements.txt file. This will allow you to install all the necessary Python packages easily.
Content of requirements.txt:
text1langchain2networkx3markdown4langchain_community
File: main.py
This is the main script containing the code for the application.
Content of main.py:
python1# main.py23import os4import networkx as nx5from langchain import PromptTemplate, LLMChain6from langchain.llms import Ollama78# Define the Node class9class Node:10 def __init__(self, node_id, prompt_text, persona):11 self.id = node_id12 self.prompt_text = prompt_text13 self.response_text = None14 self.context = ""15 self.persona = persona1617# Initialize the graph18G = nx.DiGraph()1920# Define personas21personas = {22 "Historian": "You are a knowledgeable historian specializing in the industrial revolution.",23 "Scientist": "You are a scientist with expertise in technological advancements.",24 "Philosopher": "You are a philosopher pondering societal impacts.",25 "Analyst": "You analyze information critically to provide insights.",26 # Add additional personas as needed27}2829# Function to collect context from predecessor nodes30def collect_context(node_id):31 predecessors = list(G.predecessors(node_id))32 context = ""33 for pred_id in predecessors:34 pred_node = G.nodes[pred_id]['data']35 if pred_node.response_text:36 context += f"From {pred_node.persona}:\n{pred_node.response_text}\n\n"37 return context3839# Function to generate responses using LangChain and Ollama40def generate_response(node):41 system_prompt = personas[node.persona]42 # Build the complete prompt43 prompt_template = PromptTemplate(44 input_variables=["system_prompt", "context", "prompt"],45 template="{system_prompt}\n\n{context}\n\n{prompt}"46 )47 # Instantiate the Ollama LLM48 llm = Ollama(49 base_url="http://localhost:11434", # Default Ollama server URL50 model="llama2", # Replace with the model you have downloaded51 )52 chain = LLMChain(llm=llm, prompt=prompt_template)53 response = chain.run(54 system_prompt=system_prompt,55 context=node.context,56 prompt=node.prompt_text57 )58 return response5960# Function to log interactions to a markdown file61def update_markdown(node):62 with open("conversation.md", "a", encoding="utf-8") as f:63 f.write(f"## Node {node.id}: {node.persona}\n\n")64 f.write(f"**Prompt:**\n\n{node.prompt_text}\n\n")65 f.write(f"**Response:**\n\n{node.response_text}\n\n---\n\n")6667# Function for nodes that perform analysis68def analyze_responses(node):69 # Collect responses from predecessor nodes70 predecessors = list(G.predecessors(node.id))71 analysis_input = ""72 for pred_id in predecessors:73 pred_node = G.nodes[pred_id]['data']74 analysis_input += f"{pred_node.persona}'s response:\n{pred_node.response_text}\n\n"7576 node.prompt_text = f"Provide an analysis comparing the following perspectives:\n\n{analysis_input}"77 node.context = "" # Analysis is based solely on the provided responses78 node.response_text = generate_response(node)79 update_markdown(node)8081# Build the graph8283# Create initial prompt nodes with different personas84node1 = Node(1, prompt_text="Discuss the impacts of the industrial revolution.", persona="Historian")85G.add_node(node1.id, data=node1)8687node2 = Node(2, prompt_text="Discuss the technological advancements during the industrial revolution.", persona="Scientist")88G.add_node(node2.id, data=node2)8990# Add edges if node2 should consider node1's context91G.add_edge(node1.id, node2.id) # node2 considers node1's context9293# Add an analysis node94node3 = Node(3, prompt_text="", persona="Analyst")95G.add_node(node3.id, data=node3)96G.add_edge(node1.id, node3.id)97G.add_edge(node2.id, node3.id)9899# Process the nodes100for node_id in nx.topological_sort(G):101 node = G.nodes[node_id]['data']102 if node.persona != "Analyst":103 node.context = collect_context(node_id)104 node.response_text = generate_response(node)105 update_markdown(node)106 else:107 analyze_responses(node)108109print("Conversation has been generated and logged to conversation.md")
3. Install Dependencies
Make sure you have all the required Python packages installed.
In your terminal, from within the langchain_graph_app directory, run:
bash1pip install -r requirements.txt
4. Install and Set Up Ollama
Install Ollama
Follow the installation instructions specific to your operating system.
-
macOS (via Homebrew):
bash1brew install ollama/tap/ollama -
Other Platforms:
Visit the Ollama GitHub repository for installation instructions.
Download a Model for Ollama
Ollama runs models locally. You need to download a model compatible with your application.
For example, to download the llama2 model:
bash1ollama pull llama2
Note: Replace llama2 with the name of the model you wish to use if different.
5. Run the Ollama Server
Before running the application, ensure that the Ollama server is running.
In a separate terminal window, run:
bash1ollama serve
This will start the Ollama server on the default port 11434.
6. Run the Application
Now you can run the application:
bash1python main.py
This will execute the script, generate the conversation, and create (or update) the conversation.md file with the prompts and responses.
7. View the Output
Open the conversation.md file to see the generated conversation:
bash1cat conversation.md
Or open it in a text editor that supports Markdown to view it with proper formatting.
Explanation of the Files and Code
main.py Overview
-
Imports:
networkx: For creating and managing the directed graph.langchain: For interacting with the language model.Ollama: LangChain's wrapper for the Ollama LLM.
-
Node Class:
- Represents each node in the graph.
- Stores the node ID, prompt text, persona, response text, and context.
-
Graph Initialization:
- A directed graph
Gis created usingnetworkx.DiGraph().
- A directed graph
-
Personas:
- A dictionary mapping persona names to their descriptions (system prompts).
- Personas influence how the LLM generates responses.
-
Functions:
collect_context(node_id): Gathers responses from predecessor nodes to form the context.generate_response(node): Uses theOllamaLLM via LangChain to generate a response based on the persona, context, and prompt.update_markdown(node): Appends the prompt and response toconversation.mdin a structured format.analyze_responses(node): Handles nodes designated for analysis, compiling predecessor responses and generating an analytical output.
-
Graph Construction:
- Nodes are added to the graph with unique IDs and associated data.
- Edges define the flow of information (i.e., which nodes' contexts are considered in generating responses).
-
Processing Nodes:
- Nodes are processed in topological order to respect dependencies.
- Responses are generated and logged for each node.
Understanding the Flow
-
Graph Creation:
- Three nodes are created: two with specific personas (
HistorianandScientist) and oneAnalyst. - Edges are added to define how context flows between nodes.
- Three nodes are created: two with specific personas (
-
Processing:
- Nodes are processed so that predecessors are handled before successors.
- For non-analyst nodes, the context is collected from predecessors, responses are generated, and the interaction is logged.
- For the analyst node, it compiles the responses from its predecessors and generates an analysis.
-
Logging:
- All prompts and responses are appended to
conversation.mdwith markdown formatting for readability.
- All prompts and responses are appended to
Sample Output in conversation.md
The conversation.md file will contain entries like:
text1## Node 1: Historian23**Prompt:**45Discuss the impacts of the industrial revolution.67**Response:**89[Historian's response...]1011---1213## Node 2: Scientist1415**Prompt:**1617Discuss the technological advancements during the industrial revolution.1819**Response:**2021[Scientist's response...]2223---2425## Node 3: Analyst2627**Prompt:**2829Provide an analysis comparing the following perspectives:3031Historian's response:32[Historian's response...]3334Scientist's response:35[Scientist's response...]3637**Response:**3839[Analyst's comparative analysis...]4041---
Additional Notes
-
Model Configuration:
- Ensure the
modelparameter in theOllamaLLM instantiation matches the model you have downloaded. - Example: If you downloaded
llama2, setmodel="llama2".
- Ensure the
-
Expanding the Graph:
- You can add more nodes and edges to create more complex conversations.
- Be mindful of the context window limitations of the LLM.
-
Error Handling:
- The script does not include extensive error handling.
- Consider adding try-except blocks around network calls, file operations, and LLM interactions.
-
Environment:
-
It's recommended to use a virtual environment to manage dependencies.
bash1python -m venv venv2source venv/bin/activate # On Windows use venv\Scripts\activate
-
-
Dependencies:
- Ensure all dependencies are properly installed and compatible with your Python interpreter.
Next Steps
-
Customize Personas and Prompts:
- Modify the
personasdictionary to add or adjust personas. - Change the
prompt_textfor each node to explore different topics.
- Modify the
-
Enhance Functionality:
- Add user input to create dynamic prompts or personas.
- Implement a GUI or web interface for interactive usage.
-
Visualization:
- Use graph visualization libraries, like
matplotliborpygraphviz, to visualize the conversation flow.
- Use graph visualization libraries, like
-
Documentation and Testing:
- Document any changes or additions you make to the code.
- Write unit tests for your functions to ensure reliability.
Recap of Commands
Project Setup
bash1# Create project directory and navigate into it2mkdir langchain_graph_app3cd langchain_graph_app45# Create the main script and requirements file6touch main.py requirements.txt78# Open the files in a text editor to add the provided code
Installing Dependencies
bash1# Install Python packages2pip install -r requirements.txt
Installing and Running Ollama
bash1# Install Ollama (if not already installed)2# For macOS:3brew install ollama/tap/ollama45# Download the desired model6ollama pull llama278# Start the Ollama server9ollama serve
Running the Application
bash1# Run the main script2python main.py34# View the generated conversation5cat conversation.md
Troubleshooting Tips
-
Ollama Server Issues:
- Ensure the Ollama server is running (
ollama serve) before running the script. - Check if the server is accessible at
http://localhost:11434.
- Ensure the Ollama server is running (
-
Model Not Found:
- Verify that the model name in the script matches the model you downloaded.
- Run
ollama listto see the available models.
-
Dependency Errors:
- Double-check that all dependencies are installed.
- Use
pip listto view installed packages.
-
Python Version:
- Ensure you're using a compatible Python version (3.7 or higher recommended).
-
Permission Issues:
- If you encounter permission errors when accessing files, ensure you have the necessary rights.
Additional Resources
-
Ollama Documentation:
-
LangChain Documentation:
-
NetworkX Documentation:
Let's extend the application to include detailed personas with stylistic attributes stored in JSON files. We'll modify the program to select a persona from a JSON file and adjust the LLM prompts to reflect the selected persona's writing style.
Overview of the Enhancement
-
Personas with Stylistic Attributes:
- Store detailed personas in JSON format.
- Each persona includes various stylistic and psychological attributes.
-
Integration with the Application:
- Load personas from JSON files.
- Modify the prompt generation to include these attributes.
- Adjust the LLM output to match the selected persona's style.
-
Implementation Steps:
- Update the File Structure:
- Create a
personasdirectory to store JSON files.
- Create a
- Modify the Code:
- Load personas from JSON files.
- Update the
generate_responsefunction to incorporate stylistic attributes.
- Provide Instructions:
- Explain how to add new personas.
- Show how to use the updated application.
- Update the File Structure:
1. Update the File Structure
In your project directory (langchain_graph_app), create a new directory called personas to store persona JSON files.
bash1mkdir personas
2. Create Persona JSON Files
Each persona will be stored as an individual JSON file within the personas directory. Let's create a sample persona.
Example Persona: "Ernest Hemingway"
Create a file named ernest_hemingway.json in the personas directory.
Content of personas/ernest_hemingway.json:
json1{2 "name": "Ernest Hemingway",3 "vocabulary_complexity": 3,4 "sentence_structure": "simple",5 "paragraph_organization": "structured",6 "idiom_usage": 2,7 "metaphor_frequency": 4,8 "simile_frequency": 5,9 "tone": "formal",10 "punctuation_style": "minimal",11 "contraction_usage": 5,12 "pronoun_preference": "first-person",13 "passive_voice_frequency": 2,14 "rhetorical_question_usage": 3,15 "list_usage_tendency": 2,16 "personal_anecdote_inclusion": 7,17 "pop_culture_reference_frequency": 1,18 "technical_jargon_usage": 2,19 "parenthetical_aside_frequency": 1,20 "humor_sarcasm_usage": 3,21 "emotional_expressiveness": 6,22 "emphatic_device_usage": 4,23 "quotation_frequency": 3,24 "analogy_usage": 5,25 "sensory_detail_inclusion": 8,26 "onomatopoeia_usage": 2,27 "alliteration_frequency": 2,28 "word_length_preference": "short",29 "foreign_phrase_usage": 3,30 "rhetorical_device_usage": 4,31 "statistical_data_usage": 1,32 "personal_opinion_inclusion": 7,33 "transition_usage": 5,34 "reader_question_frequency": 2,35 "imperative_sentence_usage": 3,36 "dialogue_inclusion": 8,37 "regional_dialect_usage": 4,38 "hedging_language_frequency": 2,39 "language_abstraction": "concrete",40 "personal_belief_inclusion": 6,41 "repetition_usage": 5,42 "subordinate_clause_frequency": 3,43 "verb_type_preference": "active",44 "sensory_imagery_usage": 8,45 "symbolism_usage": 6,46 "digression_frequency": 2,47 "formality_level": 5,48 "reflection_inclusion": 7,49 "irony_usage": 3,50 "neologism_frequency": 1,51 "ellipsis_usage": 2,52 "cultural_reference_inclusion": 3,53 "stream_of_consciousness_usage": 2,54 "openness_to_experience": 8,55 "conscientiousness": 6,56 "extraversion": 5,57 "agreeableness": 7,58 "emotional_stability": 6,59 "dominant_motivations": "adventure",60 "core_values": "courage",61 "decision_making_style": "intuitive",62 "empathy_level": 7,63 "self_confidence": 8,64 "risk_taking_tendency": 9,65 "idealism_vs_realism": "realistic",66 "conflict_resolution_style": "assertive",67 "relationship_orientation": "independent",68 "emotional_response_tendency": "calm",69 "creativity_level": 8,70 "age": "Late 50s",71 "gender": "Male",72 "education_level": "High School",73 "professional_background": "Writer and Journalist",74 "cultural_background": "American",75 "primary_language": "English",76 "language_fluency": "Native"77}
3. Modify main.py to Load and Use Personas
3.1. Import JSON and OS Modules
Add imports at the top of main.py:
python1import json2import os
3.2. Update the Node Class
Modify the Node class to include a persona_attributes field:
python1class Node:2 def __init__(self, node_id, prompt_text, persona_name):3 self.id = node_id4 self.prompt_text = prompt_text5 self.response_text = None6 self.context = ""7 self.persona_name = persona_name8 self.persona_attributes = {}
3.3. Load Personas from JSON Files
Create a function to load personas from the personas directory:
python1def load_personas(persona_dir):2 personas = {}3 for filename in os.listdir(persona_dir):4 if filename.endswith('.json'):5 filepath = os.path.join(persona_dir, filename)6 with open(filepath, 'r', encoding='utf-8') as f:7 persona_data = json.load(f)8 name = persona_data.get('name')9 if name:10 personas[name] = persona_data11 return personas
Load the personas after defining the function:
python1# Load personas2persona_dir = 'personas' # Directory where persona JSON files are stored3personas = load_personas(persona_dir)
3.4. Update the generate_response Function
Modify the generate_response function to include persona attributes in the system prompt:
python1def generate_response(node):2 persona = personas.get(node.persona_name)3 if not persona:4 raise ValueError(f"Persona '{node.persona_name}' not found.")56 node.persona_attributes = persona78 # Build the system prompt based on persona attributes9 system_prompt = build_system_prompt(persona)1011 # Build the complete prompt12 prompt_template = PromptTemplate(13 input_variables=["system_prompt", "context", "prompt"],14 template="{system_prompt}\n\n{context}\n\n{prompt}"15 )16 # Instantiate the Ollama LLM17 llm = Ollama(18 base_url="http://localhost:11434", # Default Ollama server URL19 model="llama2", # Replace with the model you have downloaded20 )21 chain = LLMChain(llm=llm, prompt=prompt_template)22 response = chain.run(23 system_prompt=system_prompt,24 context=node.context,25 prompt=node.prompt_text26 )27 return response
3.5. Create the build_system_prompt Function
This function constructs the system prompt using the persona's attributes.
python1def build_system_prompt(persona):2 # Construct descriptive sentences based on persona attributes3 # We'll focus on key attributes for brevity4 name = persona.get('name', 'The speaker')5 tone = persona.get('tone', 'neutral')6 sentence_structure = persona.get('sentence_structure', 'varied')7 vocabulary_complexity = persona.get('vocabulary_complexity', 5)8 formality_level = persona.get('formality_level', 5)9 pronoun_preference = persona.get('pronoun_preference', 'third-person')10 language_abstraction = persona.get('language_abstraction', 'mixed')1112 # Create a description13 description = (14 f"You are {name}, writing in a {tone} tone using {sentence_structure} sentences. "15 f"Your vocabulary complexity is {vocabulary_complexity}/10, and your formality level is {formality_level}/10. "16 f"You prefer {pronoun_preference} narration and your language abstraction is {language_abstraction}."17 )1819 # Include any other attributes as needed20 # ...2122 return description
3.6. Update the Graph Definition
Update the creation of nodes to use the new persona names.
For example, replace the old personas with the new ones:
python1# Create initial prompt nodes with different personas2node1 = Node(1, prompt_text="Discuss the impacts of the industrial revolution.", persona_name="Ernest Hemingway")3G.add_node(node1.id, data=node1)45# You can create more nodes with different personas6node2 = Node(2, prompt_text="Explain quantum mechanics in simple terms.", persona_name="Albert Einstein")7G.add_node(node2.id, data=node2)89# Add edges between nodes if needed10# G.add_edge(node1.id, node2.id)1112# Add an analysis node (you can define a generic analyst persona)13node3 = Node(3, prompt_text="", persona_name="Analyst")14G.add_node(node3.id, data=node3)15G.add_edge(node1.id, node3.id)16G.add_edge(node2.id, node3.id)
3.7. Create an Analyst Persona
Create an analyst persona JSON file analyst.json or handle the analyst within the code.
Option 1: Create personas/analyst.json
json1{2 "name": "Analyst",3 "tone": "formal",4 "sentence_structure": "complex",5 "vocabulary_complexity": 7,6 "formality_level": 8,7 "pronoun_preference": "third-person",8 "language_abstraction": "mixed"9}
Option 2: If the analyst persona is generic, handle the absence of specific attributes in build_system_prompt by providing defaults.
4. Run the Updated Application
4.1. Ensure All Persona Files are Created
Make sure you've created the necessary persona JSON files in the personas directory.
ernest_hemingway.json(as above)analyst.json(if needed)- Additional personas (e.g.,
albert_einstein.jsonif you use that persona)
4.2. Install Any New Dependencies
If you haven't already imported the json module, ensure it's included in your code. No additional installations are required as json and os are part of the Python standard library.
4.3. Run the Application
bash1python main.py
This will generate the conversation with responses tailored to the selected personas.
5. View the Output
Check the conversation.md file to see the prompts and responses with the new personas.
6. Additional Personas
To add more personas, create new JSON files in the personas directory with the required attributes.
Example: "Albert Einstein" Persona
Create personas/albert_einstein.json:
json1{2 "name": "Albert Einstein",3 "vocabulary_complexity": 8,4 "sentence_structure": "complex",5 "paragraph_organization": "structured",6 "idiom_usage": 3,7 "metaphor_frequency": 6,8 "simile_frequency": 5,9 "tone": "academic",10 "punctuation_style": "conventional",11 "contraction_usage": 2,12 "pronoun_preference": "first-person",13 "passive_voice_frequency": 6,14 "rhetorical_question_usage": 4,15 "list_usage_tendency": 3,16 "personal_anecdote_inclusion": 5,17 "technical_jargon_usage": 9,18 "parenthetical_aside_frequency": 2,19 "humor_sarcasm_usage": 4,20 "emotional_expressiveness": 5,21 "emphatic_device_usage": 6,22 "quotation_frequency": 3,23 "analogy_usage": 7,24 "sensory_detail_inclusion": 4,25 "onomatopoeia_usage": 1,26 "alliteration_frequency": 2,27 "word_length_preference": "long",28 "foreign_phrase_usage": 5,29 "rhetorical_device_usage": 7,30 "statistical_data_usage": 8,31 "personal_opinion_inclusion": 6,32 "transition_usage": 7,33 "reader_question_frequency": 2,34 "imperative_sentence_usage": 2,35 "dialogue_inclusion": 3,36 "regional_dialect_usage": 1,37 "hedging_language_frequency": 5,38 "language_abstraction": "abstract",39 "personal_belief_inclusion": 6,40 "repetition_usage": 3,41 "subordinate_clause_frequency": 7,42 "verb_type_preference": "active",43 "sensory_imagery_usage": 3,44 "symbolism_usage": 5,45 "digression_frequency": 2,46 "formality_level": 8,47 "reflection_inclusion": 7,48 "irony_usage": 2,49 "neologism_frequency": 3,50 "ellipsis_usage": 2,51 "cultural_reference_inclusion": 3,52 "stream_of_consciousness_usage": 2,53 "openness_to_experience": 9,54 "conscientiousness": 7,55 "extraversion": 4,56 "agreeableness": 6,57 "emotional_stability": 7,58 "dominant_motivations": "knowledge",59 "core_values": "integrity",60 "decision_making_style": "analytical",61 "empathy_level": 7,62 "self_confidence": 8,63 "risk_taking_tendency": 6,64 "idealism_vs_realism": "idealistic",65 "conflict_resolution_style": "collaborative",66 "relationship_orientation": "independent",67 "emotional_response_tendency": "calm",68 "creativity_level": 9,69 "age": "Mid 40s",70 "gender": "Male",71 "education_level": "Doctorate",72 "professional_background": "Physicist",73 "cultural_background": "German-American",74 "primary_language": "German",75 "language_fluency": "Fluent in English"76}
7. Testing and Refinement
7.1. Test the Application
Run the application again and observe the differences in the responses based on the personas.
7.2. Refine the build_system_prompt Function
You can enhance the build_system_prompt function to incorporate more attributes and create more nuanced system prompts.
For example:
python1def build_system_prompt(persona):2 attributes = []34 # Add tone5 tone = persona.get('tone')6 if tone:7 attributes.append(f"Your tone is {tone}.")89 # Add vocabulary complexity10 vocab_complexity = persona.get('vocabulary_complexity')11 if vocab_complexity:12 attributes.append(f"Your vocabulary complexity is rated {vocab_complexity}/10.")1314 # Add sentence structure15 sentence_structure = persona.get('sentence_structure')16 if sentence_structure:17 attributes.append(f"You use {sentence_structure} sentence structures.")1819 # Add more attributes as needed20 # ...2122 description = ' '.join(attributes)23 return f"You are {persona.get('name', 'a speaker')}. {description}"
7.3. Adjusting Prompts for LLM
Ensure that the system prompt is concise but informative. Overly long prompts may exceed the LLM's context window or lead to less coherent responses.
8. Considerations and Best Practices
-
LLM Limitations:
- The LLM's ability to mimic detailed stylistic attributes may vary.
- Some attributes may have a more pronounced effect on the output than others.
-
Prompt Engineering:
- Experiment with how you convey persona attributes in the prompt.
- You may need to adjust wording to achieve the desired effect.
-
Performance:
- Loading many large persona files might impact performance.
- Consider optimizing persona file sizes if necessary.
-
Error Handling:
- Add error handling for missing attributes or files.
- Validate persona data when loading.
9. Summary of Changes
-
File Structure:
- Added a
personasdirectory containing JSON files for each persona.
- Added a
-
Code Modifications:
- Added functions to load personas from JSON files.
- Updated the
Nodeclass to handle persona attributes. - Modified
generate_responseto include persona attributes in system prompts. - Created
build_system_promptto construct system prompts from persona attributes. - Adjusted node creation to reference personas by name from the JSON files.
-
Usage:
- Personas can now be added or modified by editing the JSON files.
- The application uses these personas to tailor LLM outputs accordingly.
10. Next Steps
-
Enhance Persona Attributes Handling:
- Implement more sophisticated mapping between persona attributes and system prompts.
- Possibly use templates or mapping dictionaries to handle various attribute values.
-
Integrate with a Database (Optional):
- If you prefer using SQLite for storing personas, you can modify the code to load personas from a SQLite database instead of JSON files.
-
User Interface:
- Create a CLI or GUI to select personas and customize prompts.
-
Logging and Analysis:
- Enhance logging to include which attributes were applied.
- Analyze how different attributes affect the generated responses.
11. Conclusion
By incorporating detailed personas with stylistic attributes, the application can generate more personalized and varied responses from the LLM. This enhancement adds depth to the generated content and allows for experimentation with different writing styles and perspectives.
Debugged final main.py
python1# main.py23import os4import networkx as nx5from langchain import PromptTemplate, LLMChain6from langchain.llms import Ollama7import json8910111213# Define the Node class14class Node:15 def __init__(self, node_id, prompt_text, persona_name):16 self.id = node_id17 self.prompt_text = prompt_text18 self.response_text = None19 self.context = ""20 self.persona_name = persona_name21 self.persona_attributes = {}2223# Initialize the graph24G = nx.DiGraph()2526# Define personas27personas = {28 "Historian": "You are a knowledgeable historian specializing in the industrial revolution.",29 "Scientist": "You are a scientist with expertise in technological advancements.",30 "Philosopher": "You are a philosopher pondering societal impacts.",31 "Analyst": "You analyze information critically to provide insights.",32 # Add additional personas as needed33}3435def load_personas(persona_dir):36 personas = {}37 for filename in os.listdir(persona_dir):38 if filename.endswith('.json'):39 filepath = os.path.join(persona_dir, filename)40 with open(filepath, 'r', encoding='utf-8') as f:41 persona_data = json.load(f)42 name = persona_data.get('name')43 if name:44 personas[name] = persona_data45 return personas4647# Load personas48persona_dir = 'personas' # Directory where persona JSON files are stored49personas = load_personas(persona_dir)5051# Function to collect context from predecessor nodes52def collect_context(node_id):53 predecessors = list(G.predecessors(node_id))54 context = ""55 for pred_id in predecessors:56 pred_node = G.nodes[pred_id]['data']57 if pred_node.response_text:58 context += f"From {pred_node.persona}:\n{pred_node.response_text}\n\n"59 return context6061# Function to generate responses using LangChain and Ollama62def generate_response(node):63 persona = personas.get(node.persona_name)64 if not persona:65 raise ValueError(f"Persona '{node.persona_name}' not found.")6667 node.persona_attributes = persona6869 # Build the system prompt based on persona attributes70 system_prompt = build_system_prompt(persona)7172 # Build the complete prompt73 prompt_template = PromptTemplate(74 input_variables=["system_prompt", "context", "prompt"],75 template="{system_prompt}\n\n{context}\n\n{prompt}"76 )77 # Instantiate the Ollama LLM78 llm = Ollama(79 base_url="http://localhost:11434", # Default Ollama server URL80 model="qwq", # Replace with the model you have downloaded81 )82 chain = LLMChain(llm=llm, prompt=prompt_template)83 response = chain.run(84 system_prompt=system_prompt,85 context=node.context,86 prompt=node.prompt_text87 )88 return response8990def build_system_prompt(persona):91 # Construct descriptive sentences based on persona attributes92 # We'll focus on key attributes for brevity93 name = persona.get('name', 'The speaker')94 tone = persona.get('tone', 'neutral')95 sentence_structure = persona.get('sentence_structure', 'varied')96 vocabulary_complexity = persona.get('vocabulary_complexity', 5)97 formality_level = persona.get('formality_level', 5)98 pronoun_preference = persona.get('pronoun_preference', 'third-person')99 language_abstraction = persona.get('language_abstraction', 'mixed')100101 # Create a description102 description = (103 f"You are {name}, writing in a {tone} tone using {sentence_structure} sentences. "104 f"Your vocabulary complexity is {vocabulary_complexity}/10, and your formality level is {formality_level}/10. "105 f"You prefer {pronoun_preference} narration and your language abstraction is {language_abstraction}."106 )107108 # Include any other attributes as needed109 # ...110111 return description112113114# Function to log interactions to a markdown file115def update_markdown(node):116 with open("conversation.md", "a", encoding="utf-8") as f:117 f.write(f"## Node {node.id}: {node.persona_name}\n\n")118 f.write(f"**Prompt:**\n\n{node.prompt_text}\n\n")119 f.write(f"**Response:**\n\n{node.response_text}\n\n---\n\n")120121# Function for nodes that perform analysis122def analyze_responses(node):123 # Collect responses from predecessor nodes124 predecessors = list(G.predecessors(node.id))125 analysis_input = ""126 for pred_id in predecessors:127 pred_node = G.nodes[pred_id]['data']128 analysis_input += f"{pred_node.persona_name}'s response:\n{pred_node.response_text}\n\n"129130 node.prompt_text = f"Provide an analysis comparing the following perspectives:\n\n{analysis_input}"131 node.context = "" # Analysis is based solely on the provided responses132 node.response_text = generate_response(node)133 update_markdown(node)134135# Build the graph136137# Create initial prompt nodes with different personas138node1 = Node(1, prompt_text="Discuss the impacts of the industrial revolution.", persona_name="Ernest Hemingway")139G.add_node(node1.id, data=node1)140141# You can create more nodes with different personas142node2 = Node(2, prompt_text="Explain quantum mechanics in simple terms.", persona_name="Albert Einstein")143G.add_node(node2.id, data=node2)144145# Add edges between nodes if needed146# G.add_edge(node1.id, node2.id)147148# Add an analysis node (you can define a generic analyst persona)149node3 = Node(3, prompt_text="", persona_name="Analyst")150G.add_node(node3.id, data=node3)151G.add_edge(node1.id, node3.id)152G.add_edge(node2.id, node3.id)153154# Process the nodes155for node_id in nx.topological_sort(G):156 node = G.nodes[node_id]['data']157 if node.persona_name != "Analyst":158 node.context = collect_context(node_id)159 node.response_text = generate_response(node)160 update_markdown(node)161 else:162 analyze_responses(node)163164print("Conversation has been generated and logged to conversation.md")
Given that a CLI is generally quicker to implement and can provide immediate utility, let's start with creating a CLI using the argparse or click library. After that, I'll provide guidance on how you can set up a GUI using Streamlit, which is a Python library that allows for quick and easy creation of web apps.
Option 1: Creating a Command Line Interface (CLI)
We'll enhance your application to allow users to:
- List available personas.
- Select personas for nodes.
- Input custom prompts.
- Customize the graph structure if desired.
1. Install the Required Libraries
We can use the argparse module from the standard library or the click library, which is more user-friendly for complex CLIs.
Let's use click:
bash1pip install click
Add this to your requirements.txt:
click
2. Modify main.py to Include CLI Functionality
2.1. Import click Module
At the top of your main.py, import click:
python1import click
2.2. Refactor the Code into Functions
We'll encapsulate the main logic into functions that we can call from the CLI commands.
Encapsulate Graph Building into a Function:
python1def build_graph(nodes_info, edges_info):2 G = nx.DiGraph()3 nodes = {}4 # Create nodes5 for node_info in nodes_info:6 node_id = node_info['id']7 prompt_text = node_info['prompt_text']8 persona_name = node_info['persona_name']9 node = Node(node_id, prompt_text, persona_name)10 G.add_node(node_id, data=node)11 nodes[node_id] = node1213 # Add edges14 for edge in edges_info:15 G.add_edge(edge['from'], edge['to'])1617 return G
Encapsulate Node Processing into a Function:
python1def process_graph(G):2 for node_id in nx.topological_sort(G):3 node = G.nodes[node_id]['data']4 if node.persona_name != "Analyst":5 node.context = collect_context(node_id, G)6 node.response_text = generate_response(node)7 update_markdown(node)8 else:9 analyze_responses(node, G)
Update the collect_context and analyze_responses functions to accept G as a parameter:
python1def collect_context(node_id, G):2 # Existing code...
python1def analyze_responses(node, G):2 # Existing code...
2.3. Create CLI Commands with click
Below all the functions, add the CLI commands:
python1@click.group()2def cli():3 pass
Command to List Available Personas:
python1@cli.command()2def list_personas():3 """List all available personas."""4 for persona_name in personas.keys():5 print(persona_name)
Command to Run the Application with Custom Inputs:
python1@cli.command()2@click.option('--nodes', '-n', default=2, help='Number of nodes (excluding the analyst node).')3def run(nodes):4 """Run the application with the specified number of nodes."""5 # Let the user select personas and input prompts for each node6 nodes_info = []7 for i in range(1, nodes + 1):8 print(f"\nConfiguring Node {i}")9 persona_name = click.prompt('Enter the persona name', type=str)10 while persona_name not in personas:11 print('Persona not found. Available personas:')12 for name in personas.keys():13 print(f" - {name}")14 persona_name = click.prompt('Enter the persona name', type=str)1516 prompt_text = click.prompt('Enter the prompt text', type=str)17 node_info = {18 'id': i,19 'prompt_text': prompt_text,20 'persona_name': persona_name21 }22 nodes_info.append(node_info)2324 # Add the analyst node25 analyst_node_id = nodes + 126 analyst_node_info = {27 'id': analyst_node_id,28 'prompt_text': '',29 'persona_name': 'Analyst'30 }31 nodes_info.append(analyst_node_info)3233 # Define edges (here we assume that the analyst node depends on all other nodes)34 edges_info = []35 for i in range(1, nodes + 1):36 edges_info.append({'from': i, 'to': analyst_node_id})3738 # Build and process the graph39 G = build_graph(nodes_info, edges_info)40 process_graph(G)41 print("\nConversation has been generated and logged to conversation.md")
2.4. Update the Main Execution Block
Replace the existing execution code at the bottom of main.py with:
python1if __name__ == '__main__':2 cli()
3. Running the Updated Application
3.1. List Available Personas
bash1python main.py list-personas
Output:
text1Ernest Hemingway2Albert Einstein3Analyst
3.2. Run the Application with Custom Inputs
bash1python main.py run --nodes 2
The application will prompt you for inputs:
text1Configuring Node 12Enter the persona name: Ernest Hemingway3Enter the prompt text: Discuss the impacts of the industrial revolution.45Configuring Node 26Enter the persona name: Albert Einstein7Enter the prompt text: Explain quantum mechanics in simple terms.89Conversation has been generated and logged to conversation.md
Option 2: Creating a Graphical User Interface (GUI) with Streamlit
Streamlit allows you to turn your Python scripts into interactive web apps with minimal effort.
1. Install Streamlit
bash1pip install streamlit
Add this to your requirements.txt:
streamlit
2. Create a New Streamlit App File
Create a new file called app.py in your project directory.
3. Write the Streamlit App
Import Necessary Modules in app.py:
python1import streamlit as st2import os3import json4import networkx as nx5from main import Node, generate_response, collect_context, analyze_responses, build_system_prompt, load_personas, update_markdown, process_graph, build_graph
Ensure main.py Functions are Importable
- Modify your
main.pyfunctions to be importable without executing the CLI commands. - Place the CLI commands under
if __name__ == '__main__':.
Load Personas
python1# Load personas2persona_dir = 'personas'3personas = load_personas(persona_dir)
Streamlit Interface
python1def main():2 st.title("LangChain Graph App")34 st.header("Create a Conversation Graph")56 # Select number of nodes7 num_nodes = st.number_input('Number of nodes (excluding the analyst node):', min_value=1, value=2)89 nodes_info = []10 for i in range(1, int(num_nodes) + 1):11 st.subheader(f"Node {i} Configuration")12 persona_name = st.selectbox(f"Select persona for Node {i}:", options=list(personas.keys()), key=f"persona_{i}")13 prompt_text = st.text_area(f"Enter the prompt for Node {i}:", key=f"prompt_{i}")14 node_info = {15 'id': i,16 'prompt_text': prompt_text,17 'persona_name': persona_name18 }19 nodes_info.append(node_info)2021 # Assume the analyst node22 analyst_node_id = int(num_nodes) + 123 analyst_node_info = {24 'id': analyst_node_id,25 'prompt_text': '',26 'persona_name': 'Analyst'27 }28 nodes_info.append(analyst_node_info)2930 # Define edges31 edges_info = []32 for i in range(1, int(num_nodes) + 1):33 edges_info.append({'from': i, 'to': analyst_node_id})3435 if st.button("Generate Conversation"):36 G = build_graph(nodes_info, edges_info)37 process_graph(G)38 st.success("Conversation has been generated and logged to conversation.md")3940 # Display the conversation41 with open("conversation.md", "r", encoding="utf-8") as f:42 content = f.read()43 st.markdown(content)
Run the Streamlit App in app.py:
python1if __name__ == '__main__':2 main()
4. Running the Streamlit App
In your terminal, run:
bash1streamlit run app.py
A web browser will open displaying your app.
5. Interact with the GUI
- Select the number of nodes.
- Choose personas from dropdown menus.
- Enter prompts for each node.
- Click "Generate Conversation" to run the application.
- The conversation will be displayed on the page.
Adjustments to main.py
To make the functions importable for the GUI, ensure that your main.py does not execute any code upon import.
- Place the CLI commands and any execution code under:
python1if __name__ == '__main__':2 cli()
Summary
-
CLI Option:
- Use
clickto create a user-friendly command-line interface. - Allows users to select personas and customize prompts interactively in the terminal.
- Use
-
GUI Option:
- Use Streamlit to build a simple web application.
- Users can select personas and enter prompts in a web browser interface.
Next Steps
-
Add Error Handling:
- Validate user inputs for personas and prompts.
- Handle exceptions that may occur during execution.
-
Enhance the GUI:
- Allow users to define custom personas through the interface.
- Provide visualization of the conversation graph.
-
Improve the CLI:
- Add more options for advanced customization.
- Save and load previous configurations.
-
Documentation:
- Update README files with instructions on how to use the CLI and GUI.
- Provide examples and screenshots for clarity.
Testing
-
CLI Testing:
- Run various configurations through the CLI to ensure functionality.
- Test with different numbers of nodes and personas.
-
GUI Testing:
- Interact with the app in the browser.
- Confirm that conversations are generated as expected.
Conclusion
By implementing either the CLI or GUI, you've enhanced your application to be more user-friendly and interactive. Users can now select personas and customize prompts without modifying the code directly.
Final main.py:
python1# main.py2import click3import os4import networkx as nx5from langchain import PromptTemplate, LLMChain6from langchain.llms import Ollama7import json8910111213# Define the Node class14class Node:15 def __init__(self, node_id, prompt_text, persona_name):16 self.id = node_id17 self.prompt_text = prompt_text18 self.response_text = None19 self.context = ""20 self.persona_name = persona_name21 self.persona_attributes = {}2223# Initialize the graph24G = nx.DiGraph()2526def build_graph(nodes_info, edges_info):27 G = nx.DiGraph()28 nodes = {}29 # Create nodes30 for node_info in nodes_info:31 node_id = node_info['id']32 prompt_text = node_info['prompt_text']33 persona_name = node_info['persona_name']34 node = Node(node_id, prompt_text, persona_name)35 G.add_node(node_id, data=node)36 nodes[node_id] = node3738 # Add edges39 for edge in edges_info:40 G.add_edge(edge['from'], edge['to'])4142 return G4344def process_graph(G):45 for node_id in nx.topological_sort(G):46 node = G.nodes[node_id]['data']47 if node.persona_name != "Analyst":48 node.context = collect_context(node_id, G)49 node.response_text = generate_response(node)50 update_markdown(node)51 else:52 analyze_responses(node, G)5354def load_personas(persona_dir):55 personas = {}56 for filename in os.listdir(persona_dir):57 if filename.endswith('.json'):58 filepath = os.path.join(persona_dir, filename)59 with open(filepath, 'r', encoding='utf-8') as f:60 persona_data = json.load(f)61 name = persona_data.get('name')62 if name:63 personas[name] = persona_data64 return personas6566# Load personas67persona_dir = 'personas' # Directory where persona JSON files are stored68personas = load_personas(persona_dir)6970# Function to collect context from predecessor nodes71def collect_context(node_id, G):72 predecessors = list(G.predecessors(node_id))73 context = ""74 for pred_id in predecessors:75 pred_node = G.nodes[pred_id]['data']76 if pred_node.response_text:77 context += f"From {pred_node.persona}:\n{pred_node.response_text}\n\n"78 return context7980# Function to generate responses using LangChain and Ollama81def generate_response(node):82 persona = personas.get(node.persona_name)83 if not persona:84 raise ValueError(f"Persona '{node.persona_name}' not found.")8586 node.persona_attributes = persona8788 # Build the system prompt based on persona attributes89 system_prompt = build_system_prompt(persona)9091 # Build the complete prompt92 prompt_template = PromptTemplate(93 input_variables=["system_prompt", "context", "prompt"],94 template="{system_prompt}\n\n{context}\n\n{prompt}"95 )96 # Instantiate the Ollama LLM97 llm = Ollama(98 base_url="http://localhost:11434", # Default Ollama server URL99 model="qwq", # Replace with the model you have downloaded100 )101 chain = LLMChain(llm=llm, prompt=prompt_template)102 response = chain.run(103 system_prompt=system_prompt,104 context=node.context,105 prompt=node.prompt_text106 )107 return response108109def build_system_prompt(persona):110 # Construct descriptive sentences based on persona attributes111 # We'll focus on key attributes for brevity112 name = persona.get('name', 'The speaker')113 tone = persona.get('tone', 'neutral')114 sentence_structure = persona.get('sentence_structure', 'varied')115 vocabulary_complexity = persona.get('vocabulary_complexity', 5)116 formality_level = persona.get('formality_level', 5)117 pronoun_preference = persona.get('pronoun_preference', 'third-person')118 language_abstraction = persona.get('language_abstraction', 'mixed')119120 # Create a description121 description = (122 f"You are {name}, writing in a {tone} tone using {sentence_structure} sentences. "123 f"Your vocabulary complexity is {vocabulary_complexity}/10, and your formality level is {formality_level}/10. "124 f"You prefer {pronoun_preference} narration and your language abstraction is {language_abstraction}."125 )126127 # Include any other attributes as needed128 # ...129130 return description131132133# Function to log interactions to a markdown file134def update_markdown(node):135 with open("conversation.md", "a", encoding="utf-8") as f:136 f.write(f"## Node {node.id}: {node.persona_name}\n\n")137 f.write(f"**Prompt:**\n\n{node.prompt_text}\n\n")138 f.write(f"**Response:**\n\n{node.response_text}\n\n---\n\n")139140# Function for nodes that perform analysis141def analyze_responses(node, G):142 # Collect responses from predecessor nodes143 predecessors = list(G.predecessors(node.id))144 analysis_input = ""145 for pred_id in predecessors:146 pred_node = G.nodes[pred_id]['data']147 analysis_input += f"{pred_node.persona_name}'s response:\n{pred_node.response_text}\n\n"148149 node.prompt_text = f"Provide an analysis comparing the following perspectives:\n\n{analysis_input}"150 node.context = "" # Analysis is based solely on the provided responses151 node.response_text = generate_response(node)152 update_markdown(node)153154155@click.group()156def cli():157 pass158159@cli.command()160def list_personas():161 """List all available personas."""162 for persona_name in personas.keys():163 print(persona_name)164165@cli.command()166@click.option('--nodes', '-n', default=2, help='Number of nodes (excluding the analyst node).')167def run(nodes):168 """Run the application with the specified number of nodes."""169 # Let the user select personas and input prompts for each node170 nodes_info = []171 for i in range(1, nodes + 1):172 print(f"\nConfiguring Node {i}")173 persona_name = click.prompt('Enter the persona name', type=str)174 while persona_name not in personas:175 print('Persona not found. Available personas:')176 for name in personas.keys():177 print(f" - {name}")178 persona_name = click.prompt('Enter the persona name', type=str)179180 prompt_text = click.prompt('Enter the prompt text', type=str)181 node_info = {182 'id': i,183 'prompt_text': prompt_text,184 'persona_name': persona_name185 }186 nodes_info.append(node_info)187188 # Add the analyst node189 analyst_node_id = nodes + 1190 analyst_node_info = {191 'id': analyst_node_id,192 'prompt_text': '',193 'persona_name': 'Analyst'194 }195 nodes_info.append(analyst_node_info)196197 # Define edges (here we assume that the analyst node depends on all other nodes)198 edges_info = []199 for i in range(1, nodes + 1):200 edges_info.append({'from': i, 'to': analyst_node_id})201202 # Build and process the graph203 G = build_graph(nodes_info, edges_info)204 process_graph(G)205 print("\nConversation has been generated and logged to conversation.md")206207if __name__ == '__main__':208 cli()
Then I added the ability to increase the number of iterations through the cli command --iterations X where X is number of iterations.
python1# main.py23import click45import os67import networkx as nx89from langchain import PromptTemplate, LLMChain1011from langchain.llms import Ollama1213import json1415161718192021# Define the Node class2223class Node:2425def __init__(self, node_id, prompt_text, persona_name):2627self.id = node_id2829self.prompt_text = prompt_text3031self.response_text = None3233self.context = ""3435self.persona_name = persona_name3637self.persona_attributes = {}38394041# Initialize the graph4243G = nx.DiGraph()44454647def build_graph(nodes_info, edges_info):4849G = nx.DiGraph()5051nodes = {}5253# Create nodes5455for node_info in nodes_info:5657node_id = node_info['id']5859prompt_text = node_info['prompt_text']6061persona_name = node_info['persona_name']6263node = Node(node_id, prompt_text, persona_name)6465G.add_node(node_id, data=node)6667nodes[node_id] = node68697071# Add edges7273for edge in edges_info:7475G.add_edge(edge['from'], edge['to'])7677return G78798081def process_graph(G, iterations):8283"""Process the graph for the specified number of iterations."""8485for iteration in range(iterations):8687print(f"\nProcessing iteration {iteration + 1}/{iterations}")8889# Store previous responses for context9091previous_responses = {}9293for node_id in G.nodes():9495node = G.nodes[node_id]['data']9697if node.response_text:9899previous_responses[node_id] = node.response_text100101102103# Process each node in topological order104105for node_id in nx.topological_sort(G):106107node = G.nodes[node_id]['data']108109if node.persona_name != "Analyst":110111node.context = collect_context(node_id, G, iteration, previous_responses)112113node.response_text = generate_response(node, iteration)114115update_markdown(node, iteration)116117else:118119analyze_responses(node, G, iteration)120121122123124def load_personas(persona_dir):125126personas = {}127128for filename in os.listdir(persona_dir):129130if filename.endswith('.json'):131132filepath = os.path.join(persona_dir, filename)133134with open(filepath, 'r', encoding='utf-8') as f:135136persona_data = json.load(f)137138name = persona_data.get('name')139140if name:141142personas[name] = persona_data143144return personas145146147148# Load personas149150persona_dir = 'personas' # Directory where persona JSON files are stored151152personas = load_personas(persona_dir)153154155156# Function to collect context from predecessor nodes157158def collect_context(node_id, G, iteration, previous_responses):159160"""Collect context including previous iterations."""161162predecessors = list(G.predecessors(node_id))163164context = ""165166# Add context from previous iterations if they exist167168if iteration > 0:169170context += f"\nPrevious iteration responses:\n"171172for pred_id in predecessors:173174if pred_id in previous_responses:175176pred_node = G.nodes[pred_id]['data']177178context += f"From {pred_node.persona_name} (previous round):\n{previous_responses[pred_id]}\n\n"179180# Add context from current iteration181182context += f"\nCurrent iteration responses:\n"183184for pred_id in predecessors:185186pred_node = G.nodes[pred_id]['data']187188if pred_node.response_text:189190context += f"From {pred_node.persona_name}:\n{pred_node.response_text}\n\n"191192return context193194195196# Function to generate responses using LangChain and Ollama197198def generate_response(node, iteration):199200"""Generate response with awareness of the current iteration."""201202persona = personas.get(node.persona_name)203204if not persona:205206raise ValueError(f"Persona '{node.persona_name}' not found.")207208node.persona_attributes = persona209210system_prompt = build_system_prompt(persona)211212# Modify the prompt to include iteration information213214iteration_prompt = f"This is round {iteration + 1} of the conversation. "215216if iteration > 0:217218iteration_prompt += "Please consider the previous responses in your reply. "219220prompt_template = PromptTemplate(221222input_variables=["system_prompt", "iteration_prompt", "context", "prompt"],223224template="{system_prompt}\n\n{iteration_prompt}\n\n{context}\n\n{prompt}"225226)227228llm = Ollama(229230base_url="http://localhost:11434",231232model="qwq",233234)235236chain = LLMChain(llm=llm, prompt=prompt_template)237238response = chain.run(239240system_prompt=system_prompt,241242iteration_prompt=iteration_prompt,243244context=node.context,245246prompt=node.prompt_text247248)249250return response251252253254def build_system_prompt(persona):255256# Construct descriptive sentences based on persona attributes257258# We'll focus on key attributes for brevity259260name = persona.get('name', 'The speaker')261262tone = persona.get('tone', 'neutral')263264sentence_structure = persona.get('sentence_structure', 'varied')265266vocabulary_complexity = persona.get('vocabulary_complexity', 5)267268formality_level = persona.get('formality_level', 5)269270pronoun_preference = persona.get('pronoun_preference', 'third-person')271272language_abstraction = persona.get('language_abstraction', 'mixed')273274275276# Create a description277278description = (279280f"You are {name}, writing in a {tone} tone using {sentence_structure} sentences. "281282f"Your vocabulary complexity is {vocabulary_complexity}/10, and your formality level is {formality_level}/10. "283284f"You prefer {pronoun_preference} narration and your language abstraction is {language_abstraction}."285286)287288289290# Include any other attributes as needed291292# ...293294295296return description297298299300301# Function to log interactions to a markdown file302303def update_markdown(node, iteration):304305"""Update markdown file with iteration information."""306307with open("conversation.md", "a", encoding="utf-8") as f:308309f.write(f"## Iteration {iteration + 1} - Node {node.id}: {node.persona_name}\n\n")310311f.write(f"**Prompt:**\n\n{node.prompt_text}\n\n")312313f.write(f"**Response:**\n\n{node.response_text}\n\n---\n\n")314315316317# Function for nodes that perform analysis318319def analyze_responses(node, G, iteration):320321"""Analyze responses with awareness of iteration context."""322323predecessors = list(G.predecessors(node.id))324325analysis_input = f"Analysis for Iteration {iteration + 1}:\n\n"326327for pred_id in predecessors:328329pred_node = G.nodes[pred_id]['data']330331analysis_input += f"{pred_node.persona_name}'s response:\n{pred_node.response_text}\n\n"332333334335node.prompt_text = (336337f"Provide an analysis comparing the following perspectives from iteration {iteration + 1}:\n\n"338339f"{analysis_input}\n"340341f"Consider how the conversation has evolved across iterations."342343)344345node.context = ""346347node.response_text = generate_response(node, iteration)348349update_markdown(node, iteration)350351352353354@click.group()355356def cli():357358pass359360361362@cli.command()363364def list_personas():365366"""List all available personas."""367368for persona_name in personas.keys():369370print(persona_name)371372373374@cli.command()375376@click.option('--nodes', '-n', default=2, help='Number of nodes (excluding the analyst node).')377378@click.option('--iterations', '-i', default=1, help='Number of conversation iterations.')379380def run(nodes, iterations):381382"""Run the application with the specified number of nodes and iterations."""383384# Clear previous conversation file385386with open("conversation.md", "w", encoding="utf-8") as f:387388f.write("# Conversation Log\n\n")389390391392# Let the user select personas and input prompts for each node393394nodes_info = []395396for i in range(1, nodes + 1):397398print(f"\nConfiguring Node {i}")399400persona_name = click.prompt('Enter the persona name', type=str)401402while persona_name not in personas:403404print('Persona not found. Available personas:')405406for name in personas.keys():407408print(f" - {name}")409410persona_name = click.prompt('Enter the persona name', type=str)411412prompt_text = click.prompt('Enter the prompt text', type=str)413414node_info = {415416'id': i,417418'prompt_text': prompt_text,419420'persona_name': persona_name421422}423424nodes_info.append(node_info)425426# Add the analyst node427428analyst_node_id = nodes + 1429430analyst_node_info = {431432'id': analyst_node_id,433434'prompt_text': '',435436'persona_name': 'Analyst'437438}439440nodes_info.append(analyst_node_info)441442# Define edges443444edges_info = []445446for i in range(1, nodes + 1):447448edges_info.append({'from': i, 'to': analyst_node_id})449450# Build and process the graph451452G = build_graph(nodes_info, edges_info)453454# Process the graph for the specified number of iterations455456process_graph(G, iterations)457458print(f"\nConversation with {iterations} iterations has been generated and logged to conversation.md")459460461462if __name__ == '__main__':463464cli()

Sovereign AI: Building Local-First Intelligent Systems
by Daniel Kliewer · Paperback · 72 pages
The hands-on guide to building AI that runs on your hardware, keeps your data private, and eliminates cloud dependence. Working code included.