Spaces:
Sleeping
Sleeping
Upload langchainAI.py
Browse files- langchainAI.py +50 -0
langchainAI.py
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from langchain.chat_models import ChatOpenAI
|
2 |
+
from langchain.chains import LLMChain
|
3 |
+
from langchain.memory import ConversationBufferMemory
|
4 |
+
from langchain.prompts import PromptTemplate
|
5 |
+
|
6 |
+
from langchain_community.chat_models import ChatOpenAI
|
7 |
+
from langchain_openai import ChatOpenAI
|
8 |
+
import os
|
9 |
+
|
10 |
+
|
11 |
+
from langchain.prompts import (
|
12 |
+
ChatPromptTemplate,
|
13 |
+
HumanMessagePromptTemplate,
|
14 |
+
MessagesPlaceholder,
|
15 |
+
)
|
16 |
+
|
17 |
+
from langchain.schema import SystemMessage
|
18 |
+
|
19 |
+
|
20 |
+
|
21 |
+
openai_api_key = os.environ.get('OPENAI_API_KEY')
|
22 |
+
|
23 |
+
chatllm = ChatOpenAI(openai_api_key=openai_api_key, temperature=0.9, model='gpt-3.5-turbo-1106')
|
24 |
+
|
25 |
+
sys_prompt = ChatPromptTemplate.from_messages(
|
26 |
+
[
|
27 |
+
SystemMessage(
|
28 |
+
content="You are a conversational helper, you often give witty, smart, and sarcastic replies in a polite and professional tone in the user's language to {conversational_text}. Always give a list of possible replies as a human."
|
29 |
+
),
|
30 |
+
MessagesPlaceholder(
|
31 |
+
variable_name="chat_history"
|
32 |
+
),
|
33 |
+
HumanMessagePromptTemplate.from_template(
|
34 |
+
"{conversational_text}"
|
35 |
+
),
|
36 |
+
]
|
37 |
+
)
|
38 |
+
|
39 |
+
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
|
40 |
+
|
41 |
+
chat_llm_chain = LLMChain(
|
42 |
+
llm=chatllm,
|
43 |
+
prompt=sys_prompt,
|
44 |
+
verbose=True,
|
45 |
+
memory=memory,
|
46 |
+
)
|
47 |
+
|
48 |
+
def get_langchain_response(input_text):
|
49 |
+
return chat_llm_chain.run(input_text)
|
50 |
+
|