diff --git a/.vscode/launch.json b/.vscode/launch.json index e8d94825..b45837ff 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -1,6 +1,19 @@ { "version": "0.2.0", "configurations": [ + { + "name": "Python: Current File", + "type": "python", + "request": "launch", + "program": "${file}", + "console": "integratedTerminal", + "justMyCode": false, + "cwd": "${workspaceFolder}/examples/langchain-chroma", + "env": { + "OPENAI_API_BASE": "http://localhost:8080/v1", + "OPENAI_API_KEY": "abc" + } + }, { "name": "Launch Go", "type": "go", diff --git a/examples/langchain-chroma/.env.example b/examples/langchain-chroma/.env.example new file mode 100644 index 00000000..37cda598 --- /dev/null +++ b/examples/langchain-chroma/.env.example @@ -0,0 +1,5 @@ +THREADS=4 +CONTEXT_SIZE=512 +MODELS_PATH=/models +DEBUG=true +# BUILD_TYPE=generic \ No newline at end of file diff --git a/examples/langchain-chroma/.gitignore b/examples/langchain-chroma/.gitignore new file mode 100644 index 00000000..3dc19014 --- /dev/null +++ b/examples/langchain-chroma/.gitignore @@ -0,0 +1,4 @@ +db/ +state_of_the_union.txt +models/bert +models/ggml-gpt4all-j \ No newline at end of file diff --git a/examples/langchain-chroma/README.md b/examples/langchain-chroma/README.md index 70e3f42b..17207a02 100644 --- a/examples/langchain-chroma/README.md +++ b/examples/langchain-chroma/README.md @@ -10,13 +10,20 @@ Download the models and start the API: # Clone LocalAI git clone https://github.com/go-skynet/LocalAI -cd LocalAI/examples/query_data +cd LocalAI/examples/langchain-chroma wget https://huggingface.co/skeskinen/ggml/resolve/main/all-MiniLM-L6-v2/ggml-model-q4_0.bin -O models/bert wget https://gpt4all.io/models/ggml-gpt4all-j.bin -O models/ggml-gpt4all-j +# configure your .env +# NOTE: ensure that THREADS does not exceed your machine's CPU cores +mv .env.example .env + # start with docker-compose docker-compose up -d --build + +# tail the logs & wait until the build completes +docker logs -f langchain-chroma-api-1 ``` ### Python requirements @@ -37,7 +44,7 @@ wget https://raw.githubusercontent.com/hwchase17/chat-your-data/master/state_of_ python store.py ``` -After it finishes, a directory "storage" will be created with the vector index database. +After it finishes, a directory "db" will be created with the vector index database. ## Query diff --git a/examples/langchain-chroma/docker-compose.yml b/examples/langchain-chroma/docker-compose.yml new file mode 100644 index 00000000..96ef540e --- /dev/null +++ b/examples/langchain-chroma/docker-compose.yml @@ -0,0 +1,15 @@ +version: '3.6' + +services: + api: + image: quay.io/go-skynet/local-ai:latest + build: + context: ../../ + dockerfile: Dockerfile + ports: + - 8080:8080 + env_file: + - ../../.env + volumes: + - ./models:/models:cached + command: ["/usr/bin/local-ai"] diff --git a/examples/langchain-chroma/models/embeddings.yaml b/examples/langchain-chroma/models/embeddings.yaml index 46a08502..536c8de1 100644 --- a/examples/langchain-chroma/models/embeddings.yaml +++ b/examples/langchain-chroma/models/embeddings.yaml @@ -1,5 +1,6 @@ name: text-embedding-ada-002 parameters: model: bert +threads: 4 backend: bert-embeddings embeddings: true diff --git a/examples/langchain-chroma/query.py b/examples/langchain-chroma/query.py index 2f7df507..33848818 100644 --- a/examples/langchain-chroma/query.py +++ b/examples/langchain-chroma/query.py @@ -2,8 +2,9 @@ import os from langchain.vectorstores import Chroma from langchain.embeddings import OpenAIEmbeddings -from langchain.llms import OpenAI -from langchain.chains import VectorDBQA +from langchain.chat_models import ChatOpenAI +from langchain.chains import RetrievalQA +from langchain.vectorstores.base import VectorStoreRetriever base_path = os.environ.get('OPENAI_API_BASE', 'http://localhost:8080/v1') @@ -12,8 +13,10 @@ embedding = OpenAIEmbeddings() persist_directory = 'db' # Now we can load the persisted database from disk, and use it as normal. +llm = ChatOpenAI(temperature=0, model_name="gpt-3.5-turbo", openai_api_base=base_path) vectordb = Chroma(persist_directory=persist_directory, embedding_function=embedding) -qa = VectorDBQA.from_chain_type(llm=OpenAI(temperature=0, model_name="gpt-3.5-turbo", openai_api_base=base_path), chain_type="stuff", vectorstore=vectordb) +retriever = VectorStoreRetriever(vectorstore=vectordb) +qa = RetrievalQA.from_llm(llm=llm, retriever=retriever) query = "What the president said about taxes ?" print(qa.run(query)) diff --git a/examples/langchain-chroma/store.py b/examples/langchain-chroma/store.py index 127bb240..b9cbad0e 100755 --- a/examples/langchain-chroma/store.py +++ b/examples/langchain-chroma/store.py @@ -2,9 +2,7 @@ import os from langchain.vectorstores import Chroma from langchain.embeddings import OpenAIEmbeddings -from langchain.text_splitter import RecursiveCharacterTextSplitter,TokenTextSplitter,CharacterTextSplitter -from langchain.llms import OpenAI -from langchain.chains import VectorDBQA +from langchain.text_splitter import CharacterTextSplitter from langchain.document_loaders import TextLoader base_path = os.environ.get('OPENAI_API_BASE', 'http://localhost:8080/v1') @@ -14,7 +12,6 @@ loader = TextLoader('state_of_the_union.txt') documents = loader.load() text_splitter = CharacterTextSplitter(chunk_size=300, chunk_overlap=70) -#text_splitter = TokenTextSplitter() texts = text_splitter.split_documents(documents) # Embed and store the texts diff --git a/examples/query_data/docker-compose.yml b/examples/query_data/docker-compose.yml index a59edfc4..cf76eb7f 100644 --- a/examples/query_data/docker-compose.yml +++ b/examples/query_data/docker-compose.yml @@ -4,7 +4,7 @@ services: api: image: quay.io/go-skynet/local-ai:latest build: - context: . + context: ../../ dockerfile: Dockerfile ports: - 8080:8080 diff --git a/examples/rwkv/.gitignore b/examples/rwkv/.gitignore new file mode 100644 index 00000000..ab3629c5 --- /dev/null +++ b/examples/rwkv/.gitignore @@ -0,0 +1,2 @@ +models/rwkv +models/rwkv.tokenizer.json \ No newline at end of file diff --git a/examples/rwkv/Dockerfile.build b/examples/rwkv/Dockerfile.build index c62024de..491f9ccd 100644 --- a/examples/rwkv/Dockerfile.build +++ b/examples/rwkv/Dockerfile.build @@ -1,5 +1,7 @@ FROM python +RUN apt-get update && apt-get -y install cmake + # convert the model (one-off) RUN pip3 install torch numpy diff --git a/go.mod b/go.mod index 960d799c..d5d97f6e 100644 --- a/go.mod +++ b/go.mod @@ -10,14 +10,14 @@ require ( github.com/go-skynet/go-bert.cpp v0.0.0-20230516063724-cea1ed76a7f4 github.com/go-skynet/go-gpt2.cpp v0.0.0-20230512145559-7bff56f02245 github.com/go-skynet/go-gpt4all-j.cpp v0.0.0-20230422090028-1f7bff57f66c - github.com/go-skynet/go-llama.cpp v0.0.0-20230516230554-b7bbefbe0b84 + github.com/go-skynet/go-llama.cpp v0.0.0-20230518171914-33f8c2db53bf github.com/gofiber/fiber/v2 v2.45.0 github.com/google/uuid v1.3.0 github.com/hashicorp/go-multierror v1.1.1 github.com/mudler/go-stable-diffusion v0.0.0-20230516152536-c0748eca3642 - github.com/nomic-ai/gpt4all/gpt4all-bindings/golang v0.0.0-20230518171731-546600fb6878 + github.com/nomic-ai/gpt4all/gpt4all-bindings/golang v0.0.0-20230518183047-94f401889042 github.com/onsi/ginkgo/v2 v2.9.5 - github.com/onsi/gomega v1.27.6 + github.com/onsi/gomega v1.27.7 github.com/otiai10/copy v1.11.0 github.com/otiai10/openaigo v1.1.0 github.com/rs/zerolog v1.29.1 diff --git a/go.sum b/go.sum index e96eb2c8..f854c399 100644 --- a/go.sum +++ b/go.sum @@ -66,6 +66,8 @@ github.com/go-skynet/go-llama.cpp v0.0.0-20230510072905-70593fccbe4b h1:qqxrjY8f github.com/go-skynet/go-llama.cpp v0.0.0-20230510072905-70593fccbe4b/go.mod h1:DLfsPD7tYYnpksERH83HSf7qVNW3FIwmz7/zfYO0/6I= github.com/go-skynet/go-llama.cpp v0.0.0-20230516230554-b7bbefbe0b84 h1:f5iYF75bAr73Tl8AdtFD5Urs/2bsHKPh52K++jLbsfk= github.com/go-skynet/go-llama.cpp v0.0.0-20230516230554-b7bbefbe0b84/go.mod h1:jxyQ26t1aKC5Gn782w9WWh5n1133PxCOfkuc01xM4RQ= +github.com/go-skynet/go-llama.cpp v0.0.0-20230518171914-33f8c2db53bf h1:D9CLQwr1eqSnV0DM7YGOKhSfNajj2qOA7XAD6+p1/HI= +github.com/go-skynet/go-llama.cpp v0.0.0-20230518171914-33f8c2db53bf/go.mod h1:oA0r4BW8ndyjTMGi1tulsNd7sdg3Ql8MaVFuT1zF6ws= github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI= github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572/go.mod h1:9Pwr4B2jHnOSGXyyzV8ROjYa2ojvAY6HCGYYfMoC3Ls= github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= @@ -115,12 +117,16 @@ github.com/nomic-ai/gpt4all/gpt4all-bindings/golang v0.0.0-20230516143155-79d624 github.com/nomic-ai/gpt4all/gpt4all-bindings/golang v0.0.0-20230516143155-79d6243fe1bc/go.mod h1:4T3CHXyrt+7FQHXaxULZfPjHbD8/99WuDDJa0YVZARI= github.com/nomic-ai/gpt4all/gpt4all-bindings/golang v0.0.0-20230518171731-546600fb6878 h1:3MFUW2a1Aqm2nMF5f+PNGq55cbxIzkRHQX/o7JVysAo= github.com/nomic-ai/gpt4all/gpt4all-bindings/golang v0.0.0-20230518171731-546600fb6878/go.mod h1:4T3CHXyrt+7FQHXaxULZfPjHbD8/99WuDDJa0YVZARI= +github.com/nomic-ai/gpt4all/gpt4all-bindings/golang v0.0.0-20230518183047-94f401889042 h1:bMU75tTgyw6mYDa5NbOHlZ1KYqQgz7heQFfTwuvCGww= +github.com/nomic-ai/gpt4all/gpt4all-bindings/golang v0.0.0-20230518183047-94f401889042/go.mod h1:4T3CHXyrt+7FQHXaxULZfPjHbD8/99WuDDJa0YVZARI= github.com/onsi/ginkgo/v2 v2.9.4 h1:xR7vG4IXt5RWx6FfIjyAtsoMAtnc3C/rFXBBd2AjZwE= github.com/onsi/ginkgo/v2 v2.9.4/go.mod h1:gCQYp2Q+kSoIj7ykSVb9nskRSsR6PUj4AiLywzIhbKM= github.com/onsi/ginkgo/v2 v2.9.5 h1:+6Hr4uxzP4XIUyAkg61dWBw8lb/gc4/X5luuxN/EC+Q= github.com/onsi/ginkgo/v2 v2.9.5/go.mod h1:tvAoo1QUJwNEU2ITftXTpR7R1RbCzoZUOs3RonqW57k= github.com/onsi/gomega v1.27.6 h1:ENqfyGeS5AX/rlXDd/ETokDz93u0YufY1Pgxuy/PvWE= github.com/onsi/gomega v1.27.6/go.mod h1:PIQNjfQwkP3aQAH7lf7j87O/5FiNr+ZR8+ipb+qQlhg= +github.com/onsi/gomega v1.27.7 h1:fVih9JD6ogIiHUN6ePK7HJidyEDpWGVB5mzM7cWNXoU= +github.com/onsi/gomega v1.27.7/go.mod h1:1p8OOlwo2iUUDsHnOrjE5UKYJ+e3W8eQ3qSlRahPmr4= github.com/otiai10/copy v1.11.0 h1:OKBD80J/mLBrwnzXqGtFCzprFSGioo30JcmR4APsNwc= github.com/otiai10/copy v1.11.0/go.mod h1:rSaLseMUsZFFbsFGc7wCJnnkTAvdc5L6VWxPE4308Ww= github.com/otiai10/mint v1.5.1 h1:XaPLeE+9vGbuyEHem1JNk3bYc7KKqyI/na0/mLd/Kks=