-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathlisting1.py
More file actions
44 lines (34 loc) · 1.15 KB
/
Copy pathlisting1.py
File metadata and controls
44 lines (34 loc) · 1.15 KB
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
'''
Created on Mar 25, 2024
@author: immanueltrummer
'''
from langchain_openai import ChatOpenAI
from langchain_core.prompts.chat import ChatPromptTemplate
from langchain_core.output_parsers.string import StrOutputParser
from langchain_core.runnables.passthrough import RunnablePassthrough
import argparse
import pandas as pd
def create_chain():
""" Creates chain for text classification.
Returns:
a chain for text classification.
"""
prompt = ChatPromptTemplate.from_template(
'{text}\n'
'Is the sentiment positive or negative?\n'
'Answer ("Positive"/"Negative")\n')
llm = ChatOpenAI(
model='gpt-4o', temperature=0,
max_tokens=1)
parser = StrOutputParser()
chain = ({'text':RunnablePassthrough()} | prompt | llm | parser)
return chain
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('file_path', type=str, help='Path to input .csv file')
args = parser.parse_args()
df = pd.read_csv(args.file_path)
chain = create_chain()
results = chain.batch(list(df['text']))
df['class'] = results
df.to_csv('result.csv')