Skip to content

Commit

Permalink
Merge pull request #83 from fractalego/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
fractalego authored Dec 23, 2023
2 parents 92f30c4 + 897ed13 commit 63dc818
Show file tree
Hide file tree
Showing 237 changed files with 2,015 additions and 9,414 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/development-tests1.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
- name: Install dependencies
run: |
sudo apt-get update
sudo apt-get install portaudio19-dev
sudo apt-get install portaudio19-dev python3-packaging
python -m pip install --upgrade pip
python -m pip install flake8 pytest
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
Expand Down
13 changes: 4 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# WAFL 0.0.70 [![Tests](https://github.com/fractalego/wafl/actions/workflows/development-tests1.yml/badge.svg)](https://github.com/fractalego/wafl/actions/workflows/development-tests1.yml)[![Docs](https://readthedocs.org/projects/wafl/badge/?version=latest)](https://wafl.readthedocs.io/en/latest/)
# WAFL 0.0.80 [![Tests](https://github.com/fractalego/wafl/actions/workflows/development-tests1.yml/badge.svg)](https://github.com/fractalego/wafl/actions/workflows/development-tests1.yml)[![Docs](https://readthedocs.org/projects/wafl/badge/?version=latest)](https://wafl.readthedocs.io/en/latest/)

Introduction
============
Expand Down Expand Up @@ -46,16 +46,11 @@ Please see the examples in the following chapters.


## LLM side (needs a GPU)

The second part is a machine that runs on a machine accessible from the interface side.
The initial configuration is for a local deployment of language models.
No action is needed to run WAFL if you want to run it as a local instance.

However, a multi-user setup will benefit for a dedicated server.
In this case, a docker image can be used
The second part (LLM side) is a model server for the speech-to-text model, the LLM, the embedding system, and the text-to-speech model.
A docker image can be used to run it as in the following:

```bash
$ docker run -p8080:8080 --env NVIDIA_DISABLE_REQUIRE=1 --gpus all fractalego/wafl-llm:latest
$ docker run -p8080:8080 --env NVIDIA_DISABLE_REQUIRE=1 --gpus all fractalego/wafl-llm:0.80
```

The interface side has a `config.json` file that needs to be filled with the IP address of the LLM side.
Expand Down
45 changes: 45 additions & 0 deletions datasets/create_rules_dataset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import asyncio
import pandas as pd

from wafl.config import Configuration
from wafl.connectors.remote.remote_llm_connector import RemoteLLMConnector


def get_prompt(df, theme):
prompt = ""
for _, row in df.sample(9).iterrows():
prompt += (
f"""
<task>
Create a plausible dialogue about the theme \"{row["Theme"]}\" based on the following summary and rules.
The rules are as follows:
{row["Rules"]}
The conversation goes as follows:
{row["Conversation"]}
</task>
""".strip()
+ "\n\n"
)

return (
prompt
+ f'<task>\nCreate plausible dialogue about the theme "{theme}" based on the following summary and rules.\n\nThe rules are as follows:\n'
)


if __name__ == "__main__":
config = Configuration.load_local_config()
remote_llm_connector = RemoteLLMConnector(
config.get_value("llm_model"), last_strings=["</task>"]
)

df = pd.read_csv("data/complex_instructions.csv")
theme = "playing a song that the user likes"
prompt = get_prompt(df, theme)
print(
asyncio.run(
remote_llm_connector.predict(prompt, temperature=0.5, num_tokens=1500)
)
)
122 changes: 122 additions & 0 deletions datasets/train_llm_on_rules_dataset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import random

import pandas as pd
from datasets import Dataset
from transformers import (
AutoTokenizer,
AutoModelForCausalLM,
TrainingArguments,
Trainer,
DataCollatorForLanguageModeling,
)

model_name_or_path = "mistralai/Mistral-7B-Instruct-v0.1"
max_length = 1024 + 512


def get_prompts(df):
prompts = []
for _, row in df.sample(frac=1).iterrows():
memory = ""
if memory == "":
memory = "The user has no memory."

current_rule = row["Rules"]
rules = df.sample(random.choice([1, 2]))["Rules"].tolist() + [current_rule]
random.shuffle(rules)
rules = "\n".join(rules)
prompt = (
f"""
The user is talking with a chatbot about the theme \"{row["Theme"]}\" based on the following summary.
<summary>
{memory}
</summary>
The rules are as follows:
<rules>
{rules}
</rules>
The conversation goes as follows:
{row["Conversation"]}
""".strip()
+ "\n\n"
)
prompts.append(prompt)

return prompts


def preprocess_function(sample):
model_inputs = tokenizer(
sample["prompt"],
return_tensors="pt",
max_length=max_length,
padding="max_length",
)
labels = tokenizer(
sample["prompt"],
return_tensors="pt",
max_length=max_length,
padding="max_length",
)

model_inputs["labels"] = labels["input_ids"]
return model_inputs


def model_init():
model = AutoModelForCausalLM.from_pretrained(model_name_or_path)
parameters = model.parameters()
for parameter in parameters:
parameter.requires_grad = False

model.model.enable_input_require_grads()
model.lm_head.training = True
for index in range(len(model.model.layers)):
model.model.layers[index].self_attn.k_proj.training = True

return model


def create_dataset_from_file(filepath):
df = pd.read_csv(filepath)
prompts = get_prompts(df)
return Dataset.from_dict({"prompt": prompts})


if __name__ == "__main__":
tokenizer = AutoTokenizer.from_pretrained(model_name_or_path)
tokenizer.pad_token = tokenizer.eos_token
dataset = create_dataset_from_file("data/complex_instructions.csv")
train_dataset = dataset.map(
preprocess_function, batched=True, batch_size=1, num_proc=4
)
data_collator = DataCollatorForLanguageModeling(tokenizer, mlm=False)
learning_rate = 1e-6
output_dir_name = f"checkpoint_lr{learning_rate}"
training_args = TrainingArguments(
output_dir=output_dir_name,
per_device_train_batch_size=1,
per_device_eval_batch_size=1,
evaluation_strategy="steps",
use_cpu=True,
learning_rate=learning_rate,
num_train_epochs=2,
logging_steps=200,
eval_steps=200,
save_total_limit=1,
)
model = model_init()
trainer = Trainer(
model=model,
args=training_args,
tokenizer=tokenizer,
data_collator=data_collator,
train_dataset=train_dataset,
)
trainer.train()
trainer.save_model("wafl-mistral")
model = trainer.model
model.push_to_hub("fractalego/wafl-mistral")
tokenizer.push_to_hub("fractalego/wafl-mistral")
Binary file not shown.
Binary file modified documentation/build/doctrees/environment.pickle
Binary file not shown.
Binary file modified documentation/build/doctrees/examples.doctree
Binary file not shown.
Binary file modified documentation/build/doctrees/index.doctree
Binary file not shown.
Binary file modified documentation/build/doctrees/introduction.doctree
Binary file not shown.
Binary file modified documentation/build/doctrees/license.doctree
Binary file not shown.
Binary file removed documentation/build/doctrees/rules.doctree
Binary file not shown.
Binary file not shown.
Binary file removed documentation/build/doctrees/wafl_init.doctree
Binary file not shown.
43 changes: 0 additions & 43 deletions documentation/build/html/_sources/directory_structure.rst.txt

This file was deleted.

6 changes: 4 additions & 2 deletions documentation/build/html/_sources/examples.rst.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ Examples
.. toctree::
:maxdepth: 2

wafl_init
directory_structure
simple_rule
rule_with_examples
rules_with_execute_command
rules_with_remember_command
7 changes: 4 additions & 3 deletions documentation/build/html/_sources/index.rst.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
You can adapt this file completely to your liking, but it should at least
contain the root `toctree` directive.
Welcome to WAFL's 0.0.45 documentation!
Welcome to WAFL's 0.0.80 documentation!
=======================================

.. toctree::
Expand All @@ -12,9 +12,10 @@ Welcome to WAFL's 0.0.45 documentation!

introduction
installation
initialization
configuration
running_WAFL
query_processing_pipeline
rules
facts_and_rules
examples
license

Expand Down
3 changes: 1 addition & 2 deletions documentation/build/html/_sources/introduction.rst.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ WAFL is a framework for personal agents.
It integrates Large language models, speech recognition and text to speech.

This framework combines Large Language Models and rules to create a predictable behavior.
Specifically, instead of organising the work of an LLM into a chain of thoughts,
WAFL intends to organise its behavior into inference trees.
A set of rules is used to define the behavior of the agent, supporting function calling and a working memory.

WAFL is a work in progress.
The current version requires the user to specify the rules to follow.
Expand Down
2 changes: 1 addition & 1 deletion documentation/build/html/_sources/license.rst.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ License

This software is licensed under the MIT License:

Copyright (c) 2023 [email protected]
Copyright (c) 2024 [email protected]

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
11 changes: 0 additions & 11 deletions documentation/build/html/_sources/rules.rst.txt

This file was deleted.

28 changes: 0 additions & 28 deletions documentation/build/html/_sources/rules_and_backtracking.rst.txt

This file was deleted.

Loading

0 comments on commit 63dc818

Please sign in to comment.