-
Notifications
You must be signed in to change notification settings - Fork 50
/
claude_flask.py
134 lines (117 loc) · 4.03 KB
/
claude_flask.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import os
from flask import Flask, request, jsonify
from claude_api import Client
#from dotenv import load_dotenv
from common.utils import *
app = Flask(__name__)
@app.route('/chat', methods=['POST'])
def create_chat():
data = request.get_json()
prompt = data['prompt']
cookie = get_cookie()
isproxy= get_proxy()
client = Client(cookie,isproxy)
conversation = client.create_new_chat()
conversation_id = conversation['uuid']
response = client.send_message(prompt, conversation_id)
return jsonify({'conversation_id': conversation_id, 'response': response})
@app.route('/chat/<conversation_id>')
def get_chat_history(conversation_id):
cookie = get_cookie()
isproxy= get_proxy()
print(isproxy)
client = Client(cookie,isproxy)
history = client.chat_conversation_history(conversation_id)
return jsonify(history)
@app.route('/send', methods=['POST'])
def send_message():
data = request.get_json()
conversation_id = data['conversation_id']
prompt = data['prompt']
cookie = get_cookie()
isproxy= get_proxy()
client = Client(cookie,isproxy)
response = client.send_message(prompt, conversation_id)
return jsonify({'response': response})
@app.route('/sendattachment', methods=['POST'])
def send_message_attachment():
conversation_id = request.form.get("conversation_id")
prompt = request.form.get("prompt")
file = request.files['file']
cookie = get_cookie()
isproxy= get_proxy()
client = Client(cookie,isproxy)
file_path = None
if file:
file_path = save_upload_file(file)
response = client.send_message(prompt, conversation_id,file_path)
return jsonify({'response': response})
@app.route('/reset', methods=['POST'])
def reset_conversations():
cookie = get_cookie()
isproxy= get_proxy()
client = Client(cookie,isproxy)
result = client.reset_all()
return jsonify({'result': result})
@app.route('/rename', methods=['POST'])
def rename_conversation():
data = request.get_json()
conversation_id = data['conversation_id']
title = data['title']
cookie = get_cookie()
isproxy= get_proxy()
client = Client(cookie,isproxy)
result = client.rename_chat(title, conversation_id)
return jsonify({'result': result})
@app.route('/upload', methods=['POST'])
def upload_attachment():
file = request.files['file']
if file:
file_path = save_upload_file(file)
cookie = get_cookie()
isproxy= get_proxy()
client = Client(cookie,isproxy)
response = client.upload_attachment(file_path)
return jsonify({'result': response})
else:
return jsonify({'error': 'No file uploaded'}), 400
def save_upload_file(file):
uploads_dir = os.getenv('uploads') # 从环境变量中获取上传文件目录
print(uploads_dir)
file_path = os.path.join(uploads_dir, file.filename)
file.save(file_path)
return file_path
@app.route('/conversations')
def list_all_conversations():
cookie = get_cookie()
isproxy= get_proxy()
client = Client(cookie,isproxy)
conversations = client.list_all_conversations()
return jsonify(conversations)
@app.route('/history/<conversation_id>')
def chat_conversation_history(conversation_id):
cookie = get_cookie()
isproxy= get_proxy()
client = Client(cookie,isproxy)
history = client.chat_conversation_history(conversation_id)
return jsonify(history)
# 加载.env文件中的环境变量
# load_dotenv()
# def get_cookie():
# #cookie = os.environ.get('cookie')
# cookie = os.getenv('cookie')
# print(cookie)
# if not cookie:
# raise ValueError("Please set the 'cookie' environment variable.")
# return cookie
# def get_proxy() -> bool:
# #cookie = os.environ.get('cookie')
# isproxy = os.getenv('ISPROXY')
# print(isproxy)1
# if not isproxy:
# return False
# else:
# return True if isproxy.lower() == 'true' else False
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=True, use_reloader=True)
app.default_encoding = 'utf-8'