Advertisement
Guest User

Untitled

a guest
Jan 13th, 2024
606
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. # config.yaml
  2. detectors:
  3. - keywords:
  4. - sk-ant-api03
  5. name: ant
  6. regex:
  7. key: sk-ant-api03-[a-zA-Z0-9_-]{95}
  8. verify:
  9. - endpoint: http://localhost:8181
  10. unsafe: true
  11.  
  12. # ant_verify.py
  13. import aiohttp
  14. import asyncio
  15. import json
  16. from aiohttp import web
  17.  
  18. key_file = open("claude_keys.txt", "a", buffering=1)
  19.  
  20. async def verify_key(request):
  21. try:
  22. # read the body
  23. body = await request.json()
  24. api_key = body['ant']['key'][0]
  25.  
  26. headers = {
  27. 'anthropic-version': '2023-06-01',
  28. 'content-type': 'application/json',
  29. 'x-api-key': api_key
  30. }
  31. data = {
  32. "model": "claude-1",
  33. "prompt": "\n\nHuman: Hello, world!\n\nAssistant:",
  34. "max_tokens_to_sample": 64,
  35. "stream": False
  36. }
  37. print(f"Got key! {api_key}")
  38. key_file.write(api_key + "\n")
  39. key_file.flush()
  40.  
  41. async with aiohttp.ClientSession() as session:
  42. async with session.post('https://api.anthropic.com/v1/complete', headers=headers, data=json.dumps(data)) as resp:
  43. if resp.status == 200:
  44. return web.Response(status=200)
  45. else:
  46. error_data = await resp.json()
  47. if error_data['error']['type'] == "authentication_error":
  48. if error_data['error']['message'] == "Invalid API Key":
  49. return web.Response(status=401, text="Invalid API Key")
  50. elif error_data['error']['message'] == "This account is not authorized to use the API. Please check with Anthropic support if you think this is in error.":
  51. return web.Response(status=403, text="API Key revoked")
  52. else:
  53. return web.Response(status=500)
  54. except Exception:
  55. return web.Response(status=400)
  56.  
  57. app = web.Application()
  58. app.router.add_post('/', verify_key)
  59.  
  60. web.run_app(app, host='localhost', port=8181)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement