14  Telephone CSV 2

from langchain_openai import ChatOpenAI
from langchain_community.document_loaders.csv_loader import CSVLoader
from langchain_core.output_parsers import StrOutputParser
import pandas as pd

llm = ChatOpenAI(model="gpt-4o-mini")
telephone2_df = pd.read_csv("data/TelephoneDepartment_2.csv")
telephone2_df.head()
Building CodeName Description Telephone
0 NaN ถามเบอร์ ถามเบอร์โทรศัพท์ใดๆ 1000
1 ER USER Counter US at ER 0338
2 ER CTER Tech CT at ER 0350, 47115
3 ER Xray ER Radiology Interpretation Room 0346, 47968
4 ER Team ER Ask Team ER 1182
from langchain.chains import create_retrieval_chain
from langchain.chains.combine_documents import create_stuff_documents_chain
from langchain_core.prompts import ChatPromptTemplate


system_prompt = f"""You are an assistant for searching hospital telephone number. Use the following <phone_book> to answer human request. 

<phone_book>
{telephone2_df.to_markdown()}
</phone_book>

If you don't know the answer, just say that you don't know. Return the output using provided <output-template>, replacing `CodeName` and `Telephone` variable with actual value.

<output-template>
**CodeName** -> `Telephone`
</output-template>
"""

prompt = ChatPromptTemplate.from_messages(
    [
        ("system", system_prompt),
        ("human", "{input}"),
    ]
)

prompt.invoke({"input": "H"}).to_messages()
[SystemMessage(content="You are an assistant for searching hospital telephone number. Use the following <phone_book> to answer human request. \n\n<phone_book>\n|    | Building   | CodeName         | Description                   | Telephone          |\n|---:|:-----------|:-----------------|:------------------------------|:-------------------|\n|  0 | nan        | ถามเบอร์          | ถามเบอร์โทรศัพท์ใดๆ              | 1000               |\n|  1 | ER         | USER             | Counter US at ER              | 0338               |\n|  2 | ER         | CTER             | Tech CT at ER                 | 0350, 47115        |\n|  3 | ER         | Xray ER          | Radiology Interpretation Room | 0346, 47968        |\n|  4 | ER         | Team ER          | Ask Team ER                   | 1182               |\n|  5 | ER         | ER1              | ER Team 1                     | 0301               |\n|  6 | ER         | ER2              | ER Team 2                     | 0303               |\n|  7 | ER         | ER3              | ER Team 3                     | 0334               |\n|  8 | ER         | Resus1           | Resusitation 1                | 1107               |\n|  9 | ER         | Resus2           | Resusitation 2                | 2282               |\n| 10 | ER ward    | 1OW              | Observe Ward 1                | 1345, 1350         |\n| 11 | ER ward    | 2OW              | Observe Ward 2                | 1334, 1339         |\n| 12 | ER ward    | 3OW              | Observe Ward 3                | 0345, 2875         |\n| 13 | ER ward    | 4IT              | ICU Trauma (4th floor)        | 2140, 2143         |\n| 14 | ER ward    | 4TW              | Trauma Ward (4th floor)       | 1521, 1531         |\n| 15 | Bldg.1     | IT - PAC         | IT PAC ในเวลา                 | 2462               |\n| 16 | Bldg.1     | IT - PAC (duty)  | IT PAC นอกเวลา                | 46365              |\n| 17 | Bldg.1     | US 2nd (Room)    | US 2nd floor (Room)           | 1340, 2497         |\n| 18 | Bldg.1     | US 2nd (Counter) | US 2nd floor (Counter)        | 1247               |\n| 19 | Sirikit    | AIMC - Gen       | AIMC - General (1st floor)    | 6700               |\n| 20 | Sirikit    | AIMC - Film Room | AIMC - Film Room              | 6710               |\n| 21 | Sirikit    | CT1              | AIMC - CT (1st floor)         | 6712               |\n| 22 | Sirikit    | MR1              | AIMC - MR (1st floor)         | 6711               |\n| 23 | Sirikit    | CT2              | AIMC - CT (2nd floor)         | 6733               |\n| 24 | Sirikit    | MR2              | AIMC - MR (2nd floor)         | 6734               |\n| 25 | SDMC       | CT - SDMC        | Tech CT at SDMC               | 3358, 080-246-1879 |\n| 26 | nan        | ดาวเหลือง         | ดาวเหลือง                      | 0153               |\n</phone_book>\n\nIf you don't know the answer, just say that you don't know. Return the output using provided <output-template>, replacing `CodeName` and `Telephone` variable with actual value.\n\n<output-template>\n**CodeName** -> `Telephone`\n</output-template>\n"),
 HumanMessage(content='H')]
chain = prompt | llm | StrOutputParser()
res1 = chain.invoke("Resusitation 1")
print(res1)
**`Resus1`** -> `1107`
res2 = chain.invoke("CT1")
print(res2)
**CT1** -> `6712`
res3 = chain.invoke("CT1, 2")
print(res3)
**CT1** -> `6712`  
**CT2** -> `6733`