-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathllm.py
More file actions
70 lines (57 loc) · 1.96 KB
/
Copy pathllm.py
File metadata and controls
70 lines (57 loc) · 1.96 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
67
68
69
import json, os, toml
from openai import OpenAI
import streamlit as st
# Load API key from credentials.txt or secrets manager
file_path = 'credentials'
if os.path.exists(file_path):
with open(file_path, 'r') as f:
secrets = toml.load(f)
else:
secrets = st.secrets
def answer(system_prompt, user_prompt, model_type="github"):
if model_type == "github":
print("Answer using Github API")
endpoint = "https://models.inference.ai.azure.com"
if 'GITHUB' not in secrets and 'GITHUB_API_KEY' not in secrets:
# throw an error if the API key is not found
raise ValueError("Github API key not found")
else:
token = secrets['GITHUB']['GITHUB_API_KEY']
elif model_type == "openrouter":
print("Answer using Openrouter API")
endpoint="https://openrouter.ai/api/v1"
if 'OPENROUTER' not in secrets and 'OPENROUTER_API_KEY' not in secrets['OPENROUTER']:
# throw an error if the API key is not found
raise ValueError("OpenRouter API key not found")
else:
token = secrets['OPENROUTER']['OPENROUTER_API_KEY']
else:
raise ValueError("Invalid API type")
model_name = "gpt-4o-mini"
client = OpenAI(
base_url=endpoint,
api_key=token,
)
response = client.chat.completions.create(
messages=[
{
"role": "system",
"content": system_prompt,
},
{
"role": "user",
"content": user_prompt,
}
],
temperature=1.0,
top_p=1.0,
max_tokens=1000,
model=model_name
)
return response.choices[0].message.content
# execute if the script is run directly
if __name__ == "__main__":
# model_type = "openrouter"
model_type = "github"
result = answer("Answer in chinese", "What is the capital of France?", model_type)
print(result)