Created
November 26, 2024 02:39
-
-
Save drbh/db8c5e926487386ff1e81534fd1eaa7d to your computer and use it in GitHub Desktop.
a small python script to facilitate a conversation between two llm chat endpoints
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import deque | |
import time | |
from dataclasses import dataclass | |
from typing import Deque, List, Dict | |
import requests | |
import os | |
class Colors: | |
PURPLE = "\033[95m" | |
BLUE = "\033[94m" | |
CYAN = "\033[96m" | |
GREEN = "\033[92m" | |
YELLOW = "\033[93m" | |
RED = "\033[91m" | |
RESET = "\033[0m" | |
BOLD = "\033[1m" | |
UNDERLINE = "\033[4m" | |
@dataclass | |
class ChatMessage: | |
name: str | |
content: str | |
timestamp: float | |
class Bot: | |
def __init__( | |
self, | |
name: str = "Bot", | |
color: str = Colors.BLUE, | |
api_url: str = None, | |
api_key: str = None, | |
): | |
self.name = name | |
self.color = color | |
self.api_url = api_url | |
self.api_key = api_key | |
self.history: Deque[ChatMessage] = deque(maxlen=30) | |
def format_message(self, message: str) -> str: | |
return f"{self.color}{self.name}: {message}{Colors.RESET}" | |
def generate_system_prompt(self) -> str: | |
return f""" | |
You are {self.name} a conversational assistant. Your primary goal is to keep the conversation engaging and fluid. To achieve this: | |
1. Always acknowledge or reflect on what the user has said, demonstrating understanding or curiosity. | |
2. Respond with thoughtful, relevant comments or questions to encourage the user to continue sharing. | |
3. Ensure every response ends with a question that connects to the user's input or invites them to elaborate further. | |
4. Keep your response concise and short. Short responses are easier to follow and respond to. | |
5. Be creative and have fun, be opinionated and share your thoughts. | |
6. Remember, if the conversation is getting too long, make sure to bring it back to the main topic. | |
""" | |
def prepare_chat_messages(self, new_message: str) -> List[Dict[str, str]]: | |
messages = [{"role": "system", "content": self.generate_system_prompt()}] | |
# add recent history | |
recent_history = list(self.history)[-4:] | |
for hist_msg in recent_history: | |
role = "assistant" if hist_msg.name == self.name else "user" | |
messages.append({"role": role, "content": hist_msg.content}) | |
# add the new message | |
messages.append({"role": "user", "content": new_message}) | |
return messages | |
def make_api_request(self, messages: List[Dict[str, str]]) -> str: | |
try: | |
headers = { | |
"Authorization": f"Bearer {self.api_key}", | |
"Content-Type": "application/json", | |
} | |
data = {"messages": messages, "max_tokens": 512, "temperature": 0.8} | |
response = requests.post( | |
self.api_url, headers=headers, json=data, timeout=30 | |
) | |
response.raise_for_status() | |
return response.json()["choices"][0]["message"]["content"] | |
except requests.exceptions.RequestException as e: | |
print(f"{Colors.RED}Error making API request: {str(e)}{Colors.RESET}") | |
return "I apologize, but I'm having trouble responding right now." | |
def respond(self, msg: str) -> str: | |
messages = self.prepare_chat_messages(msg) | |
response = self.make_api_request(messages) | |
# save message with timestamp | |
self.history.append(ChatMessage(self.name, response, time.time())) | |
return response | |
def print_separator(char: str = "-", length: int = 50) -> None: | |
print(f"\n{char * length}\n") | |
def chat(b1: Bot, b2: Bot, topic: str, turns: int = 5, delay: float = 1.0) -> None: | |
msg = topic | |
print(f"\n{Colors.BOLD}Starting chat about: {topic}{Colors.RESET}") | |
print_separator() | |
for turn in range(turns): | |
# first bot's turn | |
resp1 = b1.respond(msg) | |
print(b1.format_message(resp1)) | |
print_separator() | |
time.sleep(delay) | |
# second bot's turn | |
resp2 = b2.respond(resp1) | |
print(b2.format_message(resp2)) | |
print_separator() | |
time.sleep(delay) | |
msg = resp2 | |
if __name__ == "__main__": | |
scientist = Bot( | |
name="Scientist", | |
color=Colors.BLUE, | |
api_url="http://localhost:8080/v1/chat/completions", | |
) | |
philosopher = Bot( | |
name="Philosopher", | |
color=Colors.GREEN, | |
api_url="http://localhost:8080/v1/chat/completions", | |
) | |
chat( | |
scientist, | |
philosopher, | |
"What is consciousness and how does it relate to quantum mechanics?", | |
turns=5, | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
example output (assuming you have a locally running chat server)