File size: 850 Bytes
10ae3c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from langchain_community.chat_models import ChatOllama
from langchain.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnablePassthrough

def generate_title(message: str) -> str:
    prompt_template = """Generate one concise and descriptive title that is not longer than 3 words. Only generate one that title. Only generate 3 words. No alternatives. No explanation. Generate this title based on the following message:

Message: {message}

Title: """

    prompt = ChatPromptTemplate.from_template(prompt_template)

    model = ChatOllama(model="mistral")

    title_chain = (
        {"message": RunnablePassthrough()}
        | prompt
        | model
        | StrOutputParser()
    )

    result = title_chain.invoke(message) 
    return result