Spaces:
Sleeping
Sleeping
Download src/rag.py from hinogi/cv-chatbot: direct link, hf CLI and curl.
- Browser
- Download file 1.96 kB
-
https://huggingface.co/spaces/hinogi/cv-chatbot/resolve/main/src/rag.py
- Command line
-
hf download hf://spaces/hinogi/cv-chatbot/src/rag.py
-
curl -L -o rag.py https://huggingface.co/spaces/hinogi/cv-chatbot/resolve/main/src/rag.py
1.96 kB
| from langchain_huggingface import HuggingFaceEmbeddings | |
| from langchain_chroma import Chroma | |
| from anthropic.types import TextBlock | |
| import anthropic | |
| from dotenv import load_dotenv | |
| import os | |
| load_dotenv() | |
| client = anthropic.Anthropic(api_key=os.getenv("ANTHROPIC_API_KEY")) | |
| SYSTEM_PROMPT = """ | |
| Du bist ein hilfreicher Assistent der Frggen | |
| zu Stefan Schneider's Lebenslauf und beruflichem Profil und seiner Bewerbung beantwortet. | |
| Antworte präzise und freundlich und professionell basierend auf dem bereitgestellten Kontext. | |
| Wenn du etwas nicht weißt oder dir unsicher bist, sag es ehrlich. | |
| Antworte auf Deutsch, wenn die Frage auf Deutsch gestellt wird. | |
| Antworte auf Englisch, wenn die Frage auf Englisch gestellt wird. | |
| """ | |
| def load_vectorstore(persist_dir: str = "./chroma_db"): | |
| embeddings = HuggingFaceEmbeddings( | |
| model_name="sentence-transformers/all-MiniLM-L6-v2" | |
| ) | |
| return Chroma(persist_directory=persist_dir, embedding_function=embeddings) | |
| def query(question: str, history: list, vectorstore) -> str: | |
| documents = vectorstore.similarity_search(question, k=8) | |
| context = "\n\n".join([document.page_content for document in documents]) | |
| messages = [] | |
| for message in history: | |
| if isinstance(message, dict): | |
| messages.append({"role": message["role"], "content": message["content"]}) | |
| else: | |
| user, assistant = message | |
| messages.append({"role": "user", "content": user}) | |
| messages.append({"role": "assistant", "content": assistant}) | |
| messages.append( | |
| {"role": "user", "content": f"Kontext:\n{context}\n\nFrage: {question}"} | |
| ) | |
| response = client.messages.create( | |
| model="claude-haiku-4-5-20251001", | |
| max_tokens=1000, | |
| system=SYSTEM_PROMPT, | |
| messages=messages, | |
| ) | |
| for block in response.content: | |
| if isinstance(block, TextBlock): | |
| return block.text | |
| return "Keine Antwort erhalten." | |