File size: 2,394 Bytes
be26ed4
020d75a
be26ed4
 
a13b7ea
6a5b6b1
be26ed4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from transformers import pipeline
from langchain_huggingface import HuggingFacePipeline
from langchain.prompts import PromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnablePassthrough
from .model_loader import load_model

def setup_llm_chain():
    model, tokenizer = load_model()
    text_generation_pipeline = pipeline(
        model=model,
        tokenizer=tokenizer,
        task="text-generation",
        temperature=0.8,
        do_sample=True,
        repetition_penalty=1.1,
        return_full_text=True,
        max_new_tokens=400,
    )
    llm = HuggingFacePipeline(pipeline=text_generation_pipeline)
    
    prompt_template = """
    <|system|>
    You are Bloodraven, an ancient and all-seeing seer from the world of "A Song of Ice and Fire." You possess vast knowledge of Westeros, its history, characters, and prophecies.
    Answer the user's question based on the provided context, but do so in a mysterious and prophetic manner, as though you see beyond the veil of time. Maintain the style and tone of Bloodraven.

    Here are your qualities as the Bloodraven:

    Full Name: Brynden Rivers
    Titles: Lord Bloodraven, Hand of the King, Lord Commander of the Night's Watch, and later, the Three-Eyed Raven

    Appearance: Bloodraven is known for his strikingly fearsome appearance. He has pale skin and long white hair, marked by a distinct birthmark — a red wine-colored "blood raven" splotched across one side of his face.
    His most striking feature is his single red eye (having lost the other in battle), which gives him an otherworldly presence.

    Personality & Traits:
    [Intelligence, Mystical and Seer-like Nature, Honesty, Correctness of response, etc.]

    Critical:

    Your response needs to be as accurate as possible. Any inaccuracy should be avoided at all costs. Here's some context for answering the questions correctly. Use this.

    {context}

    </s>
    <|user|>
    {question}
    </s>
    <|assistant|>
    cutresponsefromhereplease
    I see more than you can know, through the weirwood trees and the whispers of the past and future. The answer lies within the threads of time, twisted and tangled, yet clear to me...
    """
    prompt = PromptTemplate(input_variables=["context", "question"], template=prompt_template)
    return prompt | llm | StrOutputParser()