add chat
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
# Simple PocketFlow Chat
|
||||
|
||||
A basic chat application using PocketFlow with OpenAI's GPT-4o model.
|
||||
|
||||
## Features
|
||||
|
||||
- Conversational chat interface in the terminal
|
||||
- Maintains full conversation history for context
|
||||
- Simple implementation demonstrating PocketFlow's node and flow concepts
|
||||
|
||||
## Run It
|
||||
|
||||
1. Make sure your OpenAI API key is set:
|
||||
```bash
|
||||
export OPENAI_API_KEY="your-api-key-here"
|
||||
```
|
||||
Alternatively, you can edit the `utils.py` file to include your API key directly.
|
||||
|
||||
2. Install requirements and run the application:
|
||||
```bash
|
||||
pip install -r requirements.txt
|
||||
python main.py
|
||||
```
|
||||
|
||||
## How It Works
|
||||
|
||||
```mermaid
|
||||
flowchart LR
|
||||
chat[ChatNode] -->|continue| chat
|
||||
```
|
||||
|
||||
The chat application uses:
|
||||
- A single `ChatNode` with a self-loop that:
|
||||
- Takes user input in the `prep` method
|
||||
- Sends the complete conversation history to GPT-4o
|
||||
- Adds responses to the conversation history
|
||||
- Loops back to continue the chat until the user types 'exit'
|
||||
|
||||
|
||||
## Files
|
||||
|
||||
- `main.py`: Implementation of the ChatNode and chat flow
|
||||
- `utils.py`: Simple wrapper for calling the OpenAI API
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
from pocketflow import Node, Flow
|
||||
from utils import call_llm
|
||||
|
||||
class ChatNode(Node):
|
||||
def prep(self, shared):
|
||||
# Initialize messages if this is the first run
|
||||
if "messages" not in shared:
|
||||
shared["messages"] = []
|
||||
print("Welcome to the chat! Type 'exit' to end the conversation.")
|
||||
|
||||
# Get user input
|
||||
user_input = input("\nYou: ")
|
||||
|
||||
# Check if user wants to exit
|
||||
if user_input.lower() == 'exit':
|
||||
return None
|
||||
|
||||
# Add user message to history
|
||||
shared["messages"].append({"role": "user", "content": user_input})
|
||||
|
||||
# Return all messages for the LLM
|
||||
return shared["messages"]
|
||||
|
||||
def exec(self, messages):
|
||||
if messages is None:
|
||||
return None
|
||||
|
||||
# Call LLM with the entire conversation history
|
||||
response = call_llm(messages)
|
||||
return response
|
||||
|
||||
def post(self, shared, prep_res, exec_res):
|
||||
if prep_res is None or exec_res is None:
|
||||
print("\nGoodbye!")
|
||||
return None # End the conversation
|
||||
|
||||
# Print the assistant's response
|
||||
print(f"\nAssistant: {exec_res}")
|
||||
|
||||
# Add assistant message to history
|
||||
shared["messages"].append({"role": "assistant", "content": exec_res})
|
||||
|
||||
# Loop back to continue the conversation
|
||||
return "continue"
|
||||
|
||||
# Create the flow with self-loop
|
||||
chat_node = ChatNode()
|
||||
chat_node - "continue" >> chat_node # Loop back to continue conversation
|
||||
|
||||
flow = Flow(start=chat_node)
|
||||
|
||||
# Start the chat
|
||||
if __name__ == "__main__":
|
||||
shared = {}
|
||||
flow.run(shared)
|
||||
@@ -0,0 +1,2 @@
|
||||
pocketflow>=0.0.1
|
||||
openai>=1.0.0
|
||||
@@ -0,0 +1,21 @@
|
||||
from openai import OpenAI
|
||||
import os
|
||||
|
||||
def call_llm(messages):
|
||||
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY", "your-api-key"))
|
||||
|
||||
response = client.chat.completions.create(
|
||||
model="gpt-4o",
|
||||
messages=messages,
|
||||
temperature=0.7
|
||||
)
|
||||
|
||||
return response.choices[0].message.content
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Test the LLM call
|
||||
messages = [{"role": "user", "content": "In a few words, what's the meaning of life?"}]
|
||||
response = call_llm(messages)
|
||||
print(f"Prompt: {messages[0]['content']}")
|
||||
print(f"Response: {response}")
|
||||
|
||||
Reference in New Issue
Block a user