mirror of
https://github.com/mudler/LocalAI.git
synced 2025-05-20 18:45:00 +00:00
Dockerized Langchain / PY example (#175)
This commit is contained in:
parent
e59bad89e7
commit
07c3aa1869
10 changed files with 137 additions and 10 deletions
5
examples/langchain/PY.Dockerfile
Normal file
5
examples/langchain/PY.Dockerfile
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
FROM python:3.10-bullseye
|
||||||
|
COPY ./langchainpy-localai-example /app
|
||||||
|
WORKDIR /app
|
||||||
|
RUN pip install --no-cache-dir -r requirements.txt
|
||||||
|
ENTRYPOINT [ "python", "./simple_demo.py" ];
|
|
@ -1,10 +1,6 @@
|
||||||
# langchain
|
# langchain
|
||||||
|
|
||||||
Example of using langchain in TypeScript, with the standard OpenAI llm module, and LocalAI.
|
Example of using langchain, with the standard OpenAI llm module, and LocalAI. Has docker compose profiles for both the Typescript and Python versions.
|
||||||
|
|
||||||
Example for python langchain to follow at a later date
|
|
||||||
|
|
||||||
Set up to make it easy to modify the `index.mts` file to look like any langchain example file.
|
|
||||||
|
|
||||||
**Please Note** - This is a tech demo example at this time. ggml-gpt4all-j has pretty terrible results for most langchain applications with the settings used in this example.
|
**Please Note** - This is a tech demo example at this time. ggml-gpt4all-j has pretty terrible results for most langchain applications with the settings used in this example.
|
||||||
|
|
||||||
|
@ -22,8 +18,11 @@ cd LocalAI/examples/langchain
|
||||||
# Download gpt4all-j to models/
|
# Download gpt4all-j to models/
|
||||||
wget https://gpt4all.io/models/ggml-gpt4all-j.bin -O models/ggml-gpt4all-j
|
wget https://gpt4all.io/models/ggml-gpt4all-j.bin -O models/ggml-gpt4all-j
|
||||||
|
|
||||||
# start with docker-compose
|
# start with docker-compose for typescript!
|
||||||
docker-compose up --build
|
docker-compose --profile ts up --build
|
||||||
|
|
||||||
|
# or start with docker-compose for python!
|
||||||
|
docker-compose --profile py up --build
|
||||||
```
|
```
|
||||||
|
|
||||||
## Copyright
|
## Copyright
|
||||||
|
|
|
@ -15,11 +15,29 @@ services:
|
||||||
- ./models:/models:cached
|
- ./models:/models:cached
|
||||||
command: ["/usr/bin/local-ai" ]
|
command: ["/usr/bin/local-ai" ]
|
||||||
|
|
||||||
langchainjs:
|
js:
|
||||||
build:
|
build:
|
||||||
context: .
|
context: .
|
||||||
dockerfile: JS.Dockerfile
|
dockerfile: JS.Dockerfile
|
||||||
|
profiles:
|
||||||
|
- js
|
||||||
|
- ts
|
||||||
|
depends_on:
|
||||||
|
- "api"
|
||||||
environment:
|
environment:
|
||||||
- 'OPENAI_API_KEY=sk-XXXXXXXXXXXXXXXXXXXX'
|
- 'OPENAI_API_KEY=sk-XXXXXXXXXXXXXXXXXXXX'
|
||||||
- 'OPENAI_API_HOST=http://api:8080/v1'
|
- 'OPENAI_API_BASE=http://api:8080/v1'
|
||||||
|
- 'MODEL_NAME=gpt-3.5-turbo' #gpt-3.5-turbo' # ggml-gpt4all-j' # ggml-koala-13B-4bit-128g'
|
||||||
|
|
||||||
|
py:
|
||||||
|
build:
|
||||||
|
context: .
|
||||||
|
dockerfile: PY.Dockerfile
|
||||||
|
profiles:
|
||||||
|
- py
|
||||||
|
depends_on:
|
||||||
|
- "api"
|
||||||
|
environment:
|
||||||
|
- 'OPENAI_API_KEY=sk-XXXXXXXXXXXXXXXXXXXX'
|
||||||
|
- 'OPENAI_API_BASE=http://api:8080/v1'
|
||||||
- 'MODEL_NAME=gpt-3.5-turbo' #gpt-3.5-turbo' # ggml-gpt4all-j' # ggml-koala-13B-4bit-128g'
|
- 'MODEL_NAME=gpt-3.5-turbo' #gpt-3.5-turbo' # ggml-gpt4all-j' # ggml-koala-13B-4bit-128g'
|
|
@ -4,7 +4,7 @@ import { Document } from "langchain/document";
|
||||||
import { initializeAgentExecutorWithOptions } from "langchain/agents";
|
import { initializeAgentExecutorWithOptions } from "langchain/agents";
|
||||||
import {Calculator} from "langchain/tools/calculator";
|
import {Calculator} from "langchain/tools/calculator";
|
||||||
|
|
||||||
const pathToLocalAi = process.env['OPENAI_API_HOST'] || 'http://api:8080/v1';
|
const pathToLocalAi = process.env['OPENAI_API_BASE'] || 'http://api:8080/v1';
|
||||||
const fakeApiKey = process.env['OPENAI_API_KEY'] || '-';
|
const fakeApiKey = process.env['OPENAI_API_KEY'] || '-';
|
||||||
const modelName = process.env['MODEL_NAME'] || 'gpt-3.5-turbo';
|
const modelName = process.env['MODEL_NAME'] || 'gpt-3.5-turbo';
|
||||||
|
|
||||||
|
|
24
examples/langchain/langchainpy-localai-example/.vscode/launch.json
vendored
Normal file
24
examples/langchain/langchainpy-localai-example/.vscode/launch.json
vendored
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
{
|
||||||
|
"version": "0.2.0",
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"name": "Python: Current File",
|
||||||
|
"type": "python",
|
||||||
|
"request": "launch",
|
||||||
|
"program": "${file}",
|
||||||
|
"console": "integratedTerminal",
|
||||||
|
"redirectOutput": true,
|
||||||
|
"justMyCode": false
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Python: Attach to Port 5678",
|
||||||
|
"type": "python",
|
||||||
|
"request": "attach",
|
||||||
|
"connect": {
|
||||||
|
"host": "localhost",
|
||||||
|
"port": 5678
|
||||||
|
},
|
||||||
|
"justMyCode": false
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
3
examples/langchain/langchainpy-localai-example/.vscode/settings.json
vendored
Normal file
3
examples/langchain/langchainpy-localai-example/.vscode/settings.json
vendored
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
{
|
||||||
|
"python.defaultInterpreterPath": "${workspaceFolder}/.venv/Scripts/python"
|
||||||
|
}
|
39
examples/langchain/langchainpy-localai-example/full_demo.py
Normal file
39
examples/langchain/langchainpy-localai-example/full_demo.py
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
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://api:8080/v1')
|
||||||
|
key = os.environ.get('OPENAI_API_KEY', '-')
|
||||||
|
model_name = os.environ.get('MODEL_NAME', 'gpt-3.5-turbo')
|
||||||
|
|
||||||
|
|
||||||
|
chat = ChatOpenAI(temperature=0, openai_api_base=base_path, openai_api_key=key, model_name=model_name, max_tokens=100)
|
||||||
|
|
||||||
|
print("Created ChatOpenAI for ", chat.model_name)
|
||||||
|
|
||||||
|
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])
|
||||||
|
|
||||||
|
print("ABOUT to execute")
|
||||||
|
|
||||||
|
# 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())
|
||||||
|
|
||||||
|
print(".");
|
|
@ -0,0 +1,32 @@
|
||||||
|
aiohttp==3.8.4
|
||||||
|
aiosignal==1.3.1
|
||||||
|
async-timeout==4.0.2
|
||||||
|
attrs==23.1.0
|
||||||
|
certifi==2022.12.7
|
||||||
|
charset-normalizer==3.1.0
|
||||||
|
colorama==0.4.6
|
||||||
|
dataclasses-json==0.5.7
|
||||||
|
debugpy==1.6.7
|
||||||
|
frozenlist==1.3.3
|
||||||
|
greenlet==2.0.2
|
||||||
|
idna==3.4
|
||||||
|
langchain==0.0.157
|
||||||
|
marshmallow==3.19.0
|
||||||
|
marshmallow-enum==1.5.1
|
||||||
|
multidict==6.0.4
|
||||||
|
mypy-extensions==1.0.0
|
||||||
|
numexpr==2.8.4
|
||||||
|
numpy==1.24.3
|
||||||
|
openai==0.27.6
|
||||||
|
openapi-schema-pydantic==1.2.4
|
||||||
|
packaging==23.1
|
||||||
|
pydantic==1.10.7
|
||||||
|
PyYAML==6.0
|
||||||
|
requests==2.29.0
|
||||||
|
SQLAlchemy==2.0.12
|
||||||
|
tenacity==8.2.2
|
||||||
|
tqdm==4.65.0
|
||||||
|
typing-inspect==0.8.0
|
||||||
|
typing_extensions==4.5.0
|
||||||
|
urllib3==1.26.15
|
||||||
|
yarl==1.9.2
|
|
@ -0,0 +1,6 @@
|
||||||
|
|
||||||
|
from langchain.llms import OpenAI
|
||||||
|
|
||||||
|
llm = OpenAI(temperature=0.9,model_name="gpt-3.5-turbo")
|
||||||
|
text = "What would be a good company name for a company that makes colorful socks?"
|
||||||
|
print(llm(text))
|
|
@ -12,6 +12,7 @@ stopwords:
|
||||||
roles:
|
roles:
|
||||||
user: " "
|
user: " "
|
||||||
system: " "
|
system: " "
|
||||||
|
backend: "gptj"
|
||||||
template:
|
template:
|
||||||
completion: completion
|
completion: completion
|
||||||
chat: completion # gpt4all
|
chat: completion # gpt4all
|
Loading…
Add table
Add a link
Reference in a new issue