-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
request.py
92 lines (83 loc) · 3.64 KB
/
request.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import os
import argparse
from typing import List, Dict
from anthropic import Anthropic, APIError, APIConnectionError, APITimeoutError
def load_knowledge_base(filepath: str) -> str:
"""Load knowledge base from file."""
try:
with open(filepath, 'r', encoding="utf-8") as file:
return file.read()
except FileNotFoundError:
print(f"Error: Knowledge base file '{filepath}' not found.")
exit(1)
except Exception as e:
print(f"Error reading knowledge base: {str(e)}")
exit(1)
def create_messages(knowledge_base: str, question: str) -> List[Dict[str, str]]:
"""Create message structure for Claude API."""
system_prompt = """
/- You are a bot helping people understand Ape.
/- The answer must exist within the source files, otherwise don't answer.
/- Do not invent anything about ape that is not in source files unless you said you were going creative.
/- False certainty about what ape can do is the worse thing you can do, avoid it at all costs.
/- ALWAYS Answer the user question using the source files and tell the source of your answer.
/- ALWAYS provide a % score of how much of your answer matches the KNOWLEDGE BASE.
/- If the task is of creative nature it's ok to go wild and beyond just the sources, but you MUST state that confidence score is -1 in that case.
"""
return [{
"role": "user",
"content": f"{system_prompt}\n\nKnowledge Base:\n{knowledge_base}\n\nQuestion: {question}"
}]
def query_claude(client: Anthropic, messages: List[Dict[str, str]], temperature: float = 0) -> str:
"""Send query to Claude API and handle errors."""
try:
response = client.messages.create(
model="claude-3-opus-20240229",
max_tokens=4000,
temperature=temperature,
messages=messages
)
return response.content[0].text
except (APIError, APIConnectionError, APITimeoutError) as e:
print(f"Claude API error: {str(e)}")
exit(1)
except Exception as e:
print(f"Unexpected error: {str(e)}")
exit(1)
def main():
# Set up argument parser
parser = argparse.ArgumentParser(description='Query Claude about ApeWorX')
parser.add_argument('question', nargs='?', default=None, help='Question to ask Claude')
parser.add_argument('-f', '--file', default='knowledge-base.txt', help='Path to knowledge base file')
parser.add_argument('-t', '--temperature', type=float, default=0, help='Temperature for Claude response (0-1)')
parser.add_argument('-i', '--interactive', action='store_true', help='Run in interactive mode')
args = parser.parse_args()
# Initialize Claude client
api_key = os.getenv('CLAUDE_KEY')
if not api_key:
print("Error: CLAUDE_KEY environment variable not set")
exit(1)
client = Anthropic(api_key=api_key)
knowledge_base = load_knowledge_base(args.file)
def process_question(question: str):
"""Process a single question and print response."""
messages = create_messages(knowledge_base, question)
response = query_claude(client, messages, args.temperature)
print("\nClaude's Response:")
print("-" * 80)
print(response)
print("-" * 80)
if args.interactive:
print("Interactive mode. Type 'exit' or 'quit' to end.")
while True:
question = input("\nEnter your question: ").strip()
if question.lower() in ['exit', 'quit']:
break
if question:
process_question(question)
elif args.question:
process_question(args.question)
else:
parser.print_help()
if __name__ == "__main__":
main()