-
Notifications
You must be signed in to change notification settings - Fork 0
/
ai.py
70 lines (58 loc) · 2.02 KB
/
ai.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
from langflow.load import run_flow_from_json
from dotenv import load_dotenv
import requests
from typing import Optional
import os
load_dotenv()
BASE_API_URL = "https://api.langflow.astra.datastax.com"
LANGFLOW_ID = "c063f01a-4a05-45a1-94e9-bc96716d7a8b"
APPLICATION_TOKEN = os.getenv("LANGFLOW_TOKEN")
# runs the ask_ai flow
def ask_ai(profile, question):
TWEAKS = {
"TextInput-rZLTH": {
"input_value": question
},
"TextInput-EO1Ku": {
"input_value": profile
},
}
result = run_flow_from_json(flow="AskAI.json",
input_value="message",
session_id="", # provide a session id if you want to use session state
fallback_to_env_vars=True, # False by default
tweaks=TWEAKS)
return (result[0].outputs[0].results["text"]. data["text"])
# macro flow inputs
def get_macros(profile, goals):
TWEAKS = {
"TextInput-qB2eg": {
"input_value": "goals"
},
"TextInput-MXtnA": {
"input_value": "profile"
},
}
return run_flow("", tweaks=TWEAKS, application_token=APPLICATION_TOKEN)
# runs the macro flow
def run_flow(message: str,
output_type: str = "chat",
input_type: str = "chat",
tweaks: Optional[dict] = None,
application_token: Optional[str] = None) -> dict:
api_url = f"{BASE_API_URL}/lf/{LANGFLOW_ID}/api/v1/run/macros"
payload = {
"input_value": message,
"output_type": output_type,
"input_type": input_type,
}
headers = None
if tweaks:
payload["tweaks"] = tweaks
if application_token:
headers = {"Authorization": "Bearer " + application_token, "Content-Type": "application/json"}
response = requests.post(api_url, json=payload, headers=headers)
return response.json()
# ["outputs"][0]["outputs"][0]["results"]["text"]["data"]["text"]
result = get_macros("name:mike, age:22, weight:70kgs, 175cm", "I want to lose weight")
print(result)