File size: 1,839 Bytes
1d63754
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
55
56
57
58
59
from langchain.base_language import BaseLanguageModel
from langchain.chat_models.openai import ChatOpenAI
from langchain.prompts.chat import (
    ChatPromptTemplate,
    HumanMessagePromptTemplate,
)
from langchain.schema import (
    AIMessage,
    OutputParserException,
    SystemMessage,
    HumanMessage
)


prompt = ChatPromptTemplate(
    input_variables=["input_response"],
    messages=[
        SystemMessage(content=
            "The user will send you a response and you need to remove the download link from it.\n"
            "Reformat the remaining message so no whitespace or half sentences are still there.\n"
            "If the response does not contain a download link, return the response as is.\n"
        ),
        HumanMessage(content="The dataset has been successfully converted to CSV format. You can download the converted file [here](sandbox:/Iris.csv)."),
        AIMessage(content="The dataset has been successfully converted to CSV format."),
        HumanMessagePromptTemplate.from_template("{input_response}")
    ]
)


async def remove_download_link(
    input_response: str, 
    llm: BaseLanguageModel,
) -> str:
    messages = prompt.format_prompt(input_response=input_response).to_messages()
    message = await llm.apredict_messages(messages)
    
    if not isinstance(message, AIMessage):
        raise OutputParserException("Expected an AIMessage")
    
    return message.content
    

async def test():
    llm = ChatOpenAI(model="gpt-3.5-turbo-0613")  # type: ignore
    
    example = "I have created the plot to your dataset.\n\nLink to the file [here](sandbox:/plot.png)."
    
    modifications = await remove_download_link(example, llm)
    
    print(modifications)


if __name__ == "__main__":
    import asyncio
    import dotenv
    dotenv.load_dotenv()
    
    asyncio.run(test())