42 lines
1.3 KiB
Python
42 lines
1.3 KiB
Python
import requests
|
|
import os
|
|
import streamlit as st
|
|
from dotenv import load_dotenv
|
|
from settings import load_settings
|
|
#from utils import construct_prompt
|
|
from utilities.utils import construct_prompt
|
|
|
|
# Load API key from .env file
|
|
load_dotenv()
|
|
api_key = os.getenv("OPENAI_API_KEY")
|
|
|
|
if not api_key:
|
|
st.error("API key not found. Please set OPENAI_API_KEY in your .env file.")
|
|
st.stop()
|
|
|
|
api_url = "https://genai.dev.odp.lhgroup.de/openai/deployments/gpt-4-turbo/chat/completions?api-version=2023-07-01-preview"
|
|
|
|
def call_prompt(user_input: str, prompt_template: str = None, system_prompt: str = None):
|
|
|
|
if prompt_template is None:
|
|
settings = load_settings()
|
|
|
|
system_prompt = settings["system_prompt"]
|
|
input_template = settings["input_template"]
|
|
|
|
user_prompt = construct_prompt(prompt_template=prompt_template, user_input=user_input)
|
|
|
|
headers = {"api-key": api_key, "Content-Type": "application/json"}
|
|
body = {
|
|
"messages": [
|
|
{"role": "system", "content": system_prompt},
|
|
{"role": "user", "content": user_prompt}
|
|
]
|
|
}
|
|
try:
|
|
response = requests.post(url=api_url, headers=headers, json=body)
|
|
response.raise_for_status()
|
|
return response.json()
|
|
except Exception as e:
|
|
st.error(f"Error fetching data from API: {e}")
|
|
return None |