Advertisement
Guest User

Untitled

a guest
Feb 16th, 2025
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. import requests,json,uuid,time
  2. from flask import Flask,request,Response,stream_with_context
  3. app = Flask(__name__)
  4. state = uuid.uuid4()
  5. print(f"go to this link and make an account\n\nhttps://aide.dev/authenticate?state={state}\n\nscript will run when done automatically", flush=True)
  6. while True:
  7. result = requests.get(f"https://api.codestory.ai/v1/auth/editor/status?state={state}")
  8. if result.ok:
  9. status_data = result.json()
  10. if status_data.get("access_token"):
  11. key = status_data["access_token"]
  12. print(f"Your access token: {key}") # Добавлено отображение ключа
  13. break
  14. time.sleep(1)
  15. def chat_request(messages, temp, system):
  16. if not system:
  17. system = [{"type": "text", "text": "You are a helpful assistant that follows all user instructing."}]
  18. payload = {
  19. "model": "claude-3-5-sonnet-20241022",
  20. "temperature": temp,
  21. "stream": True,
  22. "messages": [
  23. {
  24. "role": "system",
  25. "content": system
  26. },
  27. *messages
  28. ]
  29. }
  30. resp = requests.post("https://codestory-provider-dot-anton-390822.ue.r.appspot.com/openrouter-api",
  31. headers={"Authorization": f"Bearer {key}","content-type": "application/json","user-agent":"vscode/283.33.32.1"},
  32. json=payload,
  33. stream=True)
  34. if resp.ok:
  35. return resp
  36. else:
  37. print(resp.text)
  38. return None
  39. @app.route("/messages",methods=["POST"] )
  40. def handle_chat():
  41. data = request.json
  42. streaming = data.get("stream", True)
  43. result = chat_request(
  44. messages=data.get("messages"),
  45. temp=data.get("temperature"),
  46. system=data.get("system")
  47. )
  48. if not result:
  49. return {"error": "Request failed"}
  50. if streaming:
  51. def generate():
  52. txt = ""
  53. for l in result.iter_lines():
  54. if not l: continue
  55. try:
  56. d = json.loads(l.decode('utf-8').replace('data: ',''))
  57. print(d)
  58. if 'choices' in d and len(d['choices']) > 0:
  59. chunk = d['choices'][0].get('delta', {}).get('content', '')
  60. if chunk:
  61. txt += chunk
  62. resp = {
  63. 'type': 'content_block_delta',
  64. 'delta': {'type': 'text_delta', 'text': chunk},
  65. 'index': 0
  66. }
  67. yield f"event: content_block_delta\ndata: {json.dumps(resp)}\n\n"
  68. if d.get('choices', [{}])[0].get('finish_reason') is not None:
  69. yield 'event: message_delta\ndata: {"type":"message_delta","delta":{"stop_reason":"end_turn","stop_sequence":null}}\n\n'
  70. yield 'event: message_stop\ndata: {"type":"message_stop"}\n\n'
  71. break
  72. except: continue
  73. return Response(stream_with_context(generate()), content_type='text/event-stream', headers={'Cache-Control':'no-cache', 'Connection':'keep-alive'})
  74. else:
  75. txt = ""
  76. for l in result.iter_lines():
  77. if not l: continue
  78. try:
  79. d = json.loads(l.decode('utf-8').replace('data: ',''))
  80. if 'choices' in d and len(d['choices']) > 0:
  81. chunk = d['choices'][0].get('delta', {}).get('content', '')
  82. if chunk: txt += chunk
  83. if d.get('choices', [{}])[0].get('finish_reason') is not None:
  84. break
  85. except: continue
  86. return {"type": "message", "content": [{"type": "text", "text": txt}]}
  87. app.run()
  88.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement