langchain_openai_api.py 1.53 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
"""
This script is designed for interacting with a local GLM3 AI model using the `ChatGLM3` class
from the `langchain_community` library. It facilitates continuous dialogue with the GLM3 model.

1. Start the Local Model Service: Before running this script, you need to execute the `api_server.py` script
to start the GLM3 model's service.
2. Run the Script: The script includes functionality for initializing the LLMChain object and obtaining AI responses,
allowing the user to input questions and receive AI answers.
3. This demo is not support for streaming.

"""
from langchain.schema.messages import HumanMessage, SystemMessage, AIMessage
from langchain_community.llms.chatglm3 import ChatGLM3


change's avatar
change committed
16
def get_ai_response(messages, user_input):
17
18
19
20
21
22
23
    endpoint_url = "http://127.0.0.1:8000/v1/chat/completions"
    llm = ChatGLM3(
        endpoint_url=endpoint_url,
        max_tokens=4096,
        prefix_messages=messages,
        top_p=0.9
    )
change's avatar
change committed
24
    ai_response = llm.invoke(user_input)
25
26
27
28
29
30
31
32
33
34
35
    return ai_response


def continuous_conversation():
    messages = [
        SystemMessage(content="You are an intelligent AI assistant, named ChatGLM3."),
    ]
    while True:
        user_input = input("Human (or 'exit' to quit): ")
        if user_input.lower() == 'exit':
            break
change's avatar
change committed
36
37
        ai_response = get_ai_response(messages, user_input)
        print("ChatGLM3: ", ai_response)
38
39
        messages += [
            HumanMessage(content=user_input),
change's avatar
change committed
40
            AIMessage(content=ai_response),
41
42
43
44
45
        ]


if __name__ == "__main__":
    continuous_conversation()