Download src/rag_builder/LLM_Inference.py from gArthur98/RAG_App: direct link, hf CLI and curl.
- Browser
- Download file 1.06 kB
-
https://huggingface.co/spaces/gArthur98/RAG_App/resolve/main/src/rag_builder/LLM_Inference.py
- Command line
-
hf download hf://spaces/gArthur98/RAG_App/src/rag_builder/LLM_Inference.py
-
curl -L -o LLM_Inference.py https://huggingface.co/spaces/gArthur98/RAG_App/resolve/main/src/rag_builder/LLM_Inference.py
1.06 kB
| import cohere | |
| import os | |
| from dotenv import load_dotenv | |
| ##this ensures | |
| load_dotenv() | |
| API_KEY = os.getenv("SECRET_API_KEY") | |
| if API_KEY is None: | |
| raise RuntimeError("SECRET_API_KEY not set in environment") | |
| model = cohere.ClientV2(API_KEY) ##trying to get me api key | |
| def get_response(query, context=""): ##creating a function for the | |
| messages = [{ | |
| "role": "system", | |
| "content": ( | |
| "You are an AI assistant. Use the context provided by the user to give the user a concise answer to their prompt. " | |
| "If the answer isn't present do not make it up, rather, inform the user that you do not know the answer" | |
| ) | |
| }] | |
| if context: # only add the context when non-empty | |
| messages.append({"role": "system", "content": context}) | |
| messages.append({"role": "user", "content": query}) | |
| response = model.chat( | |
| model="command-a-03-2025", | |
| messages=messages | |
| ) | |
| return response.message.content[0].text.strip() | |
| ## testing model: | |
| get_response("How are you?", "Just reply") |