mirror of
https://github.com/mudler/LocalAI.git
synced 2025-06-29 22:20:43 +00:00
32 lines
1.1 KiB
Python
32 lines
1.1 KiB
Python
import os
|
|
from langchain.chat_models import ChatOpenAI
|
|
from langchain import PromptTemplate, LLMChain
|
|
from langchain.prompts.chat import (
|
|
ChatPromptTemplate,
|
|
SystemMessagePromptTemplate,
|
|
AIMessagePromptTemplate,
|
|
HumanMessagePromptTemplate,
|
|
)
|
|
from langchain.schema import (
|
|
AIMessage,
|
|
HumanMessage,
|
|
SystemMessage
|
|
)
|
|
|
|
print('Langchain + LocalAI PYTHON Tests')
|
|
|
|
base_path = os.environ.get('OPENAI_API_BASE', 'http://192.168.10.131:8080/v1')
|
|
key = os.environ.get('OPENAI_API_KEY', '-')
|
|
|
|
chat = ChatOpenAI(temperature=0, openai_api_base=base_path, openai_api_key=key, model_name="ggml-gpt4all-j")
|
|
|
|
template = "You are a helpful assistant that translates {input_language} to {output_language}."
|
|
system_message_prompt = SystemMessagePromptTemplate.from_template(template)
|
|
human_template = "{text}"
|
|
human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)
|
|
|
|
chat_prompt = ChatPromptTemplate.from_messages([system_message_prompt, human_message_prompt])
|
|
|
|
# get a chat completion from the formatted messages
|
|
chat(chat_prompt.format_prompt(input_language="English", output_language="French", text="I love programming.").to_messages())
|
|
|