Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@onepointconsulting
Created June 2, 2023 12:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save onepointconsulting/9e6cd460257f3aa4ff0205c47830f750 to your computer and use it in GitHub Desktop.
Save onepointconsulting/9e6cd460257f3aa4ff0205c47830f750 to your computer and use it in GitHub Desktop.
import sys
import os
import re
from pathlib import Path
from datetime import datetime
from langchain.chat_models import ChatOpenAI
from langchain.schema import (
HumanMessage
)
# Configuration
# Put here your API key
os.environ["OPENAI_API_KEY"] = '<key>'
# Put here your model
# Other options are 'gpt-3.5-turbo-0301'
# model_name = "gpt-3.5-turbo"
model_name = "gpt-3.5-turbo-0301"
# Initialize the chat object.
chat = ChatOpenAI(model_name=model_name, temperature=0)
print("Please enter your input to talk to ChatGPT. Enter 'q' to quit")
def generate_iso_date():
current_date = datetime.now()
return re.sub(r"\.\d+$", "", current_date.isoformat().replace(':', ''))
class ChatFile:
def __init__(self, current_file: Path, model_name: str) -> None:
self.current_file = current_file
self.model_name = model_name
print(f"Writing to file {current_file}")
with open(self.current_file, 'w') as f:
f.write(f"Langchain Session at {generate_iso_date()} with {self.model_name}\n\n")
def store_to_file(self, question: str, answer: str):
print(f"{answer}")
with open(self.current_file, 'a') as f:
f.write(f"{generate_iso_date()}:\nQ: { question}\nA: {answer}\n\n")
chat_file = ChatFile(Path(f"{model_name}_{generate_iso_date()}.txt"), model_name)
for line in sys.stdin:
print(f"[{model_name}]", end =">> ")
question = line.strip()
if 'q' == question:
break
# The LLM takes a prompt as an input and outputs a completion
resp = chat([HumanMessage(content=question)])
answer = resp.content
chat_file.store_to_file(question, answer)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment