Spaces:
Sleeping
Sleeping
Download api_client.py from tbels89/Prometheus: direct link, hf CLI and curl.
- Browser
- Download file 1.96 kB
-
https://huggingface.co/spaces/tbels89/Prometheus/resolve/main/api_client.py
- Command line
-
hf download hf://spaces/tbels89/Prometheus/api_client.py
-
curl -L -o api_client.py https://huggingface.co/spaces/tbels89/Prometheus/resolve/main/api_client.py
1.96 kB
| # api_client.py | |
| from logging_config import logger | |
| from token_counter import count_tokens | |
| from openai import OpenAI | |
| import streamlit as st | |
| # Model selection (add settings here and log) | |
| MODEL_NAME = "gpt-4o-mini" | |
| temperature = 0.3 | |
| logger.info(f"Selected model: {MODEL_NAME}, temperature: {temperature}") | |
| def send_query_to_llm(messages): | |
| """ | |
| Send the constructed messages to the LLM and get a response. | |
| Args: | |
| messages (list): List of dictionaries forming the prompt for the LLM. | |
| Returns: | |
| str: The assistant's response or a fallback message if an error occurs. | |
| """ | |
| try: | |
| # Count input tokens | |
| token_data = count_tokens(messages, "o200k_base") | |
| logger.info(f"Input prompt tokens: {token_data['total']}") | |
| logger.info(f"Token breakdown (input): {token_data['by_role']}") | |
| # Initialize OpenAI client | |
| client = OpenAI(api_key=st.secrets["OPENAI_API_KEY"]) | |
| # Send prompt to LLM | |
| logger.info(f"Sending query to model: {MODEL_NAME}") | |
| completion = client.chat.completions.create( | |
| model=MODEL_NAME, | |
| messages=messages, | |
| temperature=temperature | |
| ) | |
| # Extract assistant's response | |
| assistant_response = completion.choices[0].message.content | |
| #logger.info(f"Assistant response: {assistant_response}") | |
| # Count output tokens | |
| output_token_data = count_tokens([{"role": "assistant", "content": assistant_response}], "o200k_base") | |
| logger.info(f"Assistant response output tokens: {output_token_data['total']}") | |
| return assistant_response | |
| except KeyError as e: | |
| logger.error(f"Missing key in secrets or configuration: {e}") | |
| return "Configuration error: Missing required API key or settings." | |
| except Exception as e: | |
| logger.error(f"Error communicating with OpenAI: {e}") | |
| return "I'm sorry, I couldn't process your request at the moment." | |