Spaces:
Sleeping
Sleeping
Download solutions/conversational_memory.py from jericrealubit/ragchatbot: direct link, hf CLI and curl.
- Browser
- Download file 1.04 kB
-
https://huggingface.co/spaces/jericrealubit/ragchatbot/resolve/main/solutions/conversational_memory.py
- Command line
-
hf download hf://spaces/jericrealubit/ragchatbot/solutions/conversational_memory.py
-
curl -L -o conversational_memory.py https://huggingface.co/spaces/jericrealubit/ragchatbot/resolve/main/solutions/conversational_memory.py
1.04 kB
| from ctransformers import AutoModelForCausalLM | |
| llm = AutoModelForCausalLM.from_pretrained( | |
| "zoltanctoth/orca_mini_3B-GGUF", model_file="orca-mini-3b.q4_0.gguf" | |
| ) | |
| def get_prompt(instruction: str, history: list[str] | None = None) -> str: | |
| system = "You are an AI assistant that gives helpful answers. You answer the question in a short and concise way." | |
| prompt = f"### System:\n{system}\n\n### User:\n" | |
| if history is not None: | |
| prompt += f"This is the conversation history: {''.join(history)}. Now answer the question: " | |
| prompt += f"{instruction}\n\n### Response:\n" | |
| print(f"Prompt created: {prompt}") | |
| return prompt | |
| history = [] | |
| question = "Which city is the capital of India?" | |
| prompt = get_prompt(question) | |
| answer = "" | |
| for word in llm(prompt, stream=True): | |
| print(word, end="", flush=True) | |
| answer += word | |
| print() | |
| history.append(answer) | |
| question = "And of the United States?" | |
| prompt = get_prompt(question, history) | |
| for word in llm(prompt, stream=True): | |
| print(word, end="", flush=True) | |
| print() | |