astra-repl 0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
ai/__init__.py ADDED
File without changes
ai/client.py ADDED
@@ -0,0 +1,87 @@
1
+ #ai/client.py
2
+
3
+ import os
4
+ import requests
5
+
6
+ from dotenv import load_dotenv
7
+ from ai.models import get_model
8
+
9
+ load_dotenv()
10
+
11
+ API_KEY = os.getenv("OPENROUTER_API_KEY")
12
+
13
+ API_URL = "https://openrouter.ai/api/v1/chat/completions"
14
+
15
+ LAST_RESPONSE = ""
16
+
17
+ def ask(prompt):
18
+
19
+ global LAST_RESPONSE
20
+
21
+ if not API_KEY:
22
+ return "OPENROUTER_API_KEY topilmadi."
23
+
24
+ try:
25
+
26
+ response = requests.post(
27
+ API_URL,
28
+ headers={
29
+ "Authorization": f"Bearer {API_KEY}",
30
+ "Content-Type": "application/json"
31
+ },
32
+ json={
33
+ "model": get_model(),
34
+ "messages": [
35
+ {
36
+ "role": "system",
37
+ "content": (
38
+ "You are Astra AI, a helpful programming assistant."
39
+ )
40
+ },
41
+ {
42
+ "role": "user",
43
+ "content": prompt
44
+ }
45
+ ]
46
+ },
47
+ timeout=60
48
+ )
49
+
50
+ if response.status_code == 429:
51
+ return (
52
+ "OpenRouter limit reached.\n"
53
+ "Try again later or change model."
54
+ )
55
+
56
+ if response.status_code == 401:
57
+ return "Invalid API key."
58
+
59
+ response.raise_for_status()
60
+
61
+ data = response.json()
62
+
63
+ LAST_RESPONSE = ( data["choices"][0]["message"]["content"]
64
+ )
65
+
66
+ return LAST_RESPONSE
67
+
68
+ except requests.exceptions.Timeout:
69
+ return "Request timeout."
70
+
71
+ except requests.exceptions.ConnectionError:
72
+ return "Connection error."
73
+
74
+ except Exception as e:
75
+ return f"Error: {e}"
76
+
77
+ def get_last_response():
78
+
79
+ return LAST_RESPONSE
80
+
81
+ def get_ai_status():
82
+
83
+ return {
84
+ "provider": "OpenRouter",
85
+ "model": get_model(),
86
+ "api_key_loaded": bool(API_KEY)
87
+ }