-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
57 lines (40 loc) · 1.14 KB
/
Copy pathapi.py
File metadata and controls
57 lines (40 loc) · 1.14 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
#!/usr/bin/env python
# coding: utf-8
# In[5]:
# LLM wrapper for inference technique testing by Hector Orozco
import requests
import os
# In[7]:
API_KEY=os.getenv("OPENAI_API_KEY")
if not API_KEY:
API_KEY=input("Enter your LLM API Key to get started: ").strip()
API_BASE="https://openai.rc.asu.edu/v1"
MODEL_NAME=os.getenv("MODEL_NAME")
if not MODEL_NAME:
MODEL_NAME=input("Enter model name: ").strip()
def llm_caller(prompt,sys="",temp=0.0,mod=None):
if mod is None:
mod=MODEL_NAME
url=f"{API_BASE}/chat/completions"
headers={
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json",
}
payload={
"model": mod,
"messages": [
{"role": "system","content": sys},
{"role": "user","content": prompt},
],
"temperature": temp,
"max_tokens": 512,
}
resp = requests.post(url, headers=headers, json=payload, timeout=240)
# handles potential crashes
if resp.status_code != 200:
return ""
try:
return resp.json()["choices"][0]["message"]["content"]
except:
return ""
# In[ ]: