Skip to content

Instantly share code, notes, and snippets.

@ahmadrosid
Created April 17, 2024 10:02
Show Gist options
  • Save ahmadrosid/e5083a05c0d01d3d5ff8bb07a3c3cec4 to your computer and use it in GitHub Desktop.
Save ahmadrosid/e5083a05c0d01d3d5ff8bb07a3c3cec4 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