-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery.py
More file actions
66 lines (52 loc) · 2.15 KB
/
query.py
File metadata and controls
66 lines (52 loc) · 2.15 KB
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
import os
import httpx
from dotenv import load_dotenv
from langchain_ollama import ChatOllama
from tools import MassBankTool
load_dotenv()
base_url = os.environ["CUSTOM_BASE_URL"] # defined in .env file
api_key = os.environ["OPENWEBUI_SECRET_KEY"]
# verify that environment variables are loaded correctly
print(f"Base URL: {base_url}")
print(f"API Key: {api_key}")
print(f"SSL_CERT_FILE: {os.environ.get('SSL_CERT_FILE')}")
# Discover available models from Ollama
with httpx.Client(base_url=base_url, headers={"Authorization": f"Bearer {api_key}"}, timeout=15.0) as s:
resp = s.get("/api/tags")
resp.raise_for_status()
models = [m.get("name") for m in resp.json().get("models", [])]
print(f"Available models: {models}")
# Chat with an Ollama model
client = ChatOllama(
model="qwen3:30b-a3b",
base_url=base_url,
client_kwargs={ # httpx ssl authentication
"headers": {
"Authorization": f"Bearer {api_key}",
},
},
# + .env file needs to contain the path to SSL_CERT_FILE when using corporate hosted chatbot
)
response = client.predict(text="What is the capital of France?")
print(response)
# --- MassBank tool integration example ---
def query_mass_of_compound_by_name(compound_name):
tool = MassBankTool(verify_ssl=False)
endpoint = "/records"
params = {"compound_name": compound_name}
try:
result = tool.call_api(endpoint, params=params)
# Try to extract mass from the first record, if available
if isinstance(result, list) and result:
record = result[0]
# Mass is usually under record['compound']['mass']
mass = record.get("compound", {}).get("mass")
return mass if mass else f"Mass not found in first record: {record}"
return f"No records found for compound: {compound_name}"
except Exception as e:
return f"Error querying MassBank: {e}"
if __name__ == "__main__":
compound_name = "Telmisartan O-acyl-glucuronide" # MSBNK-IPB_Halle-PB001341
print(f"Querying mass for compound name: {compound_name} ...")
mass = query_mass_of_compound_by_name(compound_name)
print(f"Mass of {compound_name}: {mass}")