Skip to content

Instantly share code, notes, and snippets.

@andhikayuana
Forked from ahmadrosid/write-fixer.py
Created April 18, 2024 03:18
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 andhikayuana/9036b82a4a10ba2a978cdd29f62954bc to your computer and use it in GitHub Desktop.
Save andhikayuana/9036b82a4a10ba2a978cdd29f62954bc to your computer and use it in GitHub Desktop.
Fixed typos, spelling mistakes, and grammar while maintaining clarity and a concise tone using Claude.
import sys
import anthropic
from dotenv import load_dotenv
load_dotenv()
client = anthropic.Anthropic()
prompt_template = """
Please review and edit the following text to ensure it is grammatically correct, clear, and concise. Aim to maintain a casual tone without being overly informal. Provide suggestions for improvement and explain any changes you make. Do not add double quotes in you rewrite answer. Answer in direct response.
"""
messages = []
def ask_claude(question):
if len(messages) == 0:
question = prompt_template + question
messages.append({"role": "user", "content": question})
with client.messages.stream(
max_tokens=1024,
messages=messages,
# model="claude-3-opus-20240229",
model="claude-3-sonnet-20240229",
) as stream:
result = ""
for text in stream.text_stream:
result += text
print(text, end="", flush=True)
messages.append({"role": "assistant", "content": result})
print("")
try:
while True:
user_input = input("Ask ai: ")
if user_input.lower() == 'exit' or user_input.lower() == 'q':
print("")
break
if user_input == '':
continue
ask_claude(question=user_input)
except KeyboardInterrupt:
print("")
except:
print("Unexpected error:", sys.exc_info()[0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment