diff --git a/.github/workflows/test-api-contract.yaml b/.github/workflows/test-api-contract.yaml
index 3707f4de..6312cc50 100644
--- a/.github/workflows/test-api-contract.yaml
+++ b/.github/workflows/test-api-contract.yaml
@@ -15,6 +15,9 @@ jobs:
- uses: actions/setup-python@v4
with:
python-version: "3.10"
+ - uses: actions/setup-node@v3
+ with:
+ node-version: 16
- run: cd oasst-shared && pip install -e .
@@ -22,9 +25,14 @@ jobs:
- run: cd backend && pip install -r requirements.txt
+ - run: cd website && npm ci
+
- run: ./scripts/backend-development/start-mock-server.sh
- - name: Run contract tests
+ - name: Run Python OasstApiClient contract tests
run: ./scripts/oasst-shared-development/test.sh
+ - name: Run JavaScript OasstApiClient contract tests
+ run: ./scripts/frontend-development/run-contract-test.sh
+
- run: ./scripts/backend-development/stop-mock-server.sh
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
new file mode 100644
index 00000000..608afe25
--- /dev/null
+++ b/CONTRIBUTING.md
@@ -0,0 +1,110 @@
+# I’m in! Now what?
+
+[Join the OpenAssistant Contributors Discord Server!](https://ykilcher.com/open-assistant-discord),
+this is for work coordination.
+
+[Join the LAION Discord Server!](https://discord.com/invite/mVcgxMPD7e), it has
+a dedicated channel and is more public.
+
+[and / or the YK Discord Server](https://ykilcher.com/discord), also has a
+dedicated, but not as active, channel.
+
+[Visit the Notion](https://ykilcher.com/open-assistant)
+
+### Taking on Tasks
+
+We have a growing task list
+[of issues](https://github.com/LAION-AI/Open-Assistant/issues). Find an issue
+that appeals to you and make a comment that you'd like to work on it. Include in
+your comment a brief description of how you'll solve the problem and if there
+are any open questions you want to discuss. Once a project coordinator has
+assigned the issue to you, start working on it.
+
+If the issue is currently unclear but you are interested, please post in Discord
+and someone can help clarify the issue with more detail.
+
+**Always Welcome:** Documentation markdowns in `docs/`, docstrings, diagrams of
+the system architecture, and other documentation.
+
+### Submitting Work
+
+We're all working on different parts of Open Assistant together. To make
+contributions smoothly we recommend the following:
+
+1. [Fork this project repository](https://docs.github.com/en/get-started/quickstart/fork-a-repo)
+ and clone it to your local machine. (Read more
+ [About Forks](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/about-forks))
+1. Before working on any changes, try to
+ [sync the forked repository](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/syncing-a-fork)
+ to keep it up-to-date with the upstream repository.
+1. Work on a small focused change that only touches on a few files.
+1. Run `pre-commit` and make sure all files have formatting fixed. This
+ simplifies life for reviewers.
+1. Package up a small bit of work that solves part of the problem
+ [into a Pull Request](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/creating-a-pull-request-from-a-fork)
+ and
+ [send it out for review](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/requesting-a-pull-request-review).
+1. If you're lucky, we can merge your change into `main` without any problems.
+ If there's changes to files you're working on, resolve them by:
+1. First try rebase as suggested
+ [in these instructions](https://timwise.co.uk/2019/10/14/merge-vs-rebase/#should-you-rebase).
+1. If rebase feels too painful, merge as suggested
+ [in these instructions](https://timwise.co.uk/2019/10/14/merge-vs-rebase/#should-you-merge).
+1. Once you've resolved any conflicts, finish the review and merge into `main`.
+1. Merge in your change and move onto a new issue or the second step of your
+ current issue.
+
+Additionally, if someone is working on an issue that interests you, ask if they
+need help on it or would like suggestions on how to approach the issue. If so,
+share wildly. If they seem to have a good handle on it, let them work on their
+solution until a challenge comes up.
+
+### When does a review finish
+
+A review finishes when all blocking comments are addressed and at least one
+owning reviewer has approved the PR. Be sure to acknowledge any non-blocking
+comments either by making the request change, explaining why it's not being
+addressed now, or filing an issue to handle it later.
+
+## Developer Setup
+
+Work is organized in the
+[project board](https://github.com/orgs/LAION-AI/projects/3).
+
+**Anything that is in the `Todo` column and not assigned, is up for grabs.
+Meaning we'd be happy for anyone to do these tasks.**
+
+If you want to work on something, assign yourself to it or write a comment that
+you want to work on it and what you plan to do.
+
+- To get started with development, if you want to work on the backend, have a
+ look at `scripts/backend-development/README.md`.
+- If you want to work on any frontend, have a look at
+ `scripts/frontend-development/README.md` to make a backend available.
+
+There is also a minimal implementation of a frontend in the `text-frontend`
+folder.
+
+We are using Python 3.10 for the backend.
+
+Check out the
+[High-Level Protocol Architecture](https://www.notion.so/High-Level-Protocol-Architecture-6f1fd3551da74213b560ead369f132dc)
+
+### Website
+
+The website is built using Next.js and is in the `website` folder.
+
+### Pre-commit
+
+Install `pre-commit` and run `pre-commit install` to install the pre-commit
+hooks.
+
+In case you haven't done this, have already committed, and CI is failing, you
+can run `pre-commit run --all-files` to run the pre-commit hooks on all files.
+
+### Deployment
+
+Upon making a release on GitHub, all docker images are automatically built and
+pushed to ghcr.io. The docker images are tagged with the release version, and
+the `latest` tag. Further, the ansible playbook in `ansible/dev.yaml` is run to
+automatically deploy the built release to the dev machine.
diff --git a/README.md b/README.md
index b619c931..a443a5d7 100644
--- a/README.md
+++ b/README.md
@@ -1,7 +1,34 @@
-# Open-Assistant
+
+ Open-Assistant
+
+
-Open Assistant is a project meant to give everyone access to a great chat based
-large language model.
+
+
+# Table of Contents
+
+- [What is Open Assistant?](#what-is-open-assistant)
+- [Do you want to try it out?](#do-you-want-to-try-it-out)
+- [The Plan](#the-plan)
+- [The Vision](#the-vision)
+- [How can you help?](#how-can-you-help)
+- [I’m in! How do I contribute?](CONTRIBUTING.md)
+
+---
+
+## What is Open Assistant?
+
+
+ Open Assistant is a project meant to give everyone access to a great chat based large language model.
+
We believe that by doing this we will create a revolution in innovation in
language. In the same way that stable-diffusion helped the world make art and
@@ -14,7 +41,7 @@ If you are interested in taking a look at the current state of the project, you
can set up an entire stack needed to run **Open-Assistant**, including the
website, backend, and associated dependent services.
-To start the demo, run this in the root directory of the repository:
+##### To start the demo, run this in the root directory of the repository:
```sh
docker compose up --build
@@ -23,22 +50,21 @@ docker compose up --build
Then, navigate to `http://localhost:3000` (It may take some time to boot up) and
interact with the website.
-**Note:** When logging in via email, navigate to `http://localhost:1080` to get
-the magic email login link.
+> **Note:** When logging in via email, navigate to `http://localhost:1080` to
+> get the magic email login link.
-**Note:** If you would like to run this in a standardized development
-environment (a
-["devcontainer"](https://code.visualstudio.com/docs/devcontainers/containers))
-using
-[vscode locally](https://code.visualstudio.com/docs/devcontainers/create-dev-container#_create-a-devcontainerjson-file)
-or in a web browser using
-[GitHub Codespaces](https://github.com/features/codespaces), you can use the
-provided [`.devcontainer`](.devcontainer/) folder.
+> **Note:** If you would like to run this in a standardized development
+> environment (a
+> ["devcontainer"](https://code.visualstudio.com/docs/devcontainers/containers))
+> using
+> [vscode locally](https://code.visualstudio.com/docs/devcontainers/create-dev-container#_create-a-devcontainerjson-file)
+> or in a web browser using
+> [GitHub Codespaces](https://github.com/features/codespaces), you can use the
+> provided [`.devcontainer`](.devcontainer/) folder.
## The Plan
-We want to get to an initial MVP as fast as possible, by following the 3-steps
-outlined in the InstructGPT paper.
+##### We want to get to an initial MVP as fast as possible, by following the 3-steps outlined in the InstructGPT paper.
1. Collect high-quality human generated Instruction-Fulfillment samples
(prompt + response), goal >50k. We design a crowdsourced process to collect
@@ -80,113 +106,4 @@ All open source projects begin with people like you. Open source is the belief
that if we collaborate we can together gift our knowledge and technology to the
world for the benefit of humanity.
-## I’m in! Now what?
-
-[Join the OpenAssistant Contributors Discord Server!](https://ykilcher.com/open-assistant-discord),
-this is for work coordination.
-
-[Join the LAION Discord Server!](https://discord.com/invite/mVcgxMPD7e), it has
-a dedicated channel and is more public.
-
-[and / or the YK Discord Server](https://ykilcher.com/discord), also has a
-dedicated, but not as active, channel.
-
-[Visit the Notion](https://ykilcher.com/open-assistant)
-
-### Taking on Tasks
-
-We have a growing task list
-[of issues](https://github.com/LAION-AI/Open-Assistant/issues). Find an issue
-that appeals to you and make a comment that you'd like to work on it. Include in
-your comment a brief description of how you'll solve the problem and if there
-are any open questions you want to discuss. Once a project coordinator has
-assigned the issue to you, start working on it.
-
-If the issue is currently unclear but you are interested, please post in Discord
-and someone can help clarify the issue with more detail.
-
-**Always Welcome:** Documentation markdowns in `docs/`, docstrings, diagrams of
-the system architecture, and other documentation.
-
-### Submitting Work
-
-We're all working on different parts of Open Assistant together. To make
-contributions smoothly we recommend the following:
-
-1. [Fork this project repository](https://docs.github.com/en/get-started/quickstart/fork-a-repo)
- and clone it to your local machine. (Read more
- [About Forks](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/about-forks))
-1. Before working on any changes, try to
- [sync the forked repository](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/syncing-a-fork)
- to keep it up-to-date with the upstream repository.
-1. Work on a small focused change that only touches on a few files.
-1. Run `pre-commit` and make sure all files have formatting fixed. This
- simplifies life for reviewers.
-1. Package up a small bit of work that solves part of the problem
- [into a Pull Request](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/creating-a-pull-request-from-a-fork)
- and
- [send it out for review](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/requesting-a-pull-request-review).
-1. If you're lucky, we can merge your change into `main` without any problems.
- If there's changes to files you're working on, resolve them by:
-1. First try rebase as suggested
- [in these instructions](https://timwise.co.uk/2019/10/14/merge-vs-rebase/#should-you-rebase).
-1. If rebase feels too painful, merge as suggested
- [in these instructions](https://timwise.co.uk/2019/10/14/merge-vs-rebase/#should-you-merge).
-1. Once you've resolved any conflicts, finish the review and merge into `main`.
-1. Merge in your change and move onto a new issue or the second step of your
- current issue.
-
-Additionally, if someone is working on an issue that interests you, ask if they
-need help on it or would like suggestions on how to approach the issue. If so,
-share wildly. If they seem to have a good handle on it, let them work on their
-solution until a challenge comes up.
-
-### When does a review finish
-
-A review finishes when all blocking comments are addressed and at least one
-owning reviewer has approved the PR. Be sure to acknowledge any non-blocking
-comments either by making the request change, explaining why it's not being
-addressed now, or filing an issue to handle it later.
-
-## Developer Setup
-
-Work is organized in the
-[project board](https://github.com/orgs/LAION-AI/projects/3).
-
-**Anything that is in the `Todo` column and not assigned, is up for grabs.
-Meaning we'd be happy for anyone to do these tasks.**
-
-If you want to work on something, assign yourself to it or write a comment that
-you want to work on it and what you plan to do.
-
-- To get started with development, if you want to work on the backend, have a
- look at `scripts/backend-development/README.md`.
-- If you want to work on any frontend, have a look at
- `scripts/frontend-development/README.md` to make a backend available.
-
-There is also a minimal implementation of a frontend in the `text-frontend`
-folder.
-
-We are using Python 3.10 for the backend.
-
-Check out the
-[High-Level Protocol Architecture](https://www.notion.so/High-Level-Protocol-Architecture-6f1fd3551da74213b560ead369f132dc)
-
-### Website
-
-The website is built using Next.js and is in the `website` folder.
-
-### Pre-commit
-
-Install `pre-commit` and run `pre-commit install` to install the pre-commit
-hooks.
-
-In case you haven't done this, have already committed, and CI is failing, you
-can run `pre-commit run --all-files` to run the pre-commit hooks on all files.
-
-### Deployment
-
-Upon making a release on GitHub, all docker images are automatically built and
-pushed to ghcr.io. The docker images are tagged with the release version, and
-the `latest` tag. Further, the ansible playbook in `ansible/dev.yaml` is run to
-automatically deploy the built release to the dev machine.
+Check out our [contributing guide](CONTRIBUTING.md) to get started.
diff --git a/assets/logo_crop.png b/assets/logo_crop.png
new file mode 100644
index 00000000..20630d6b
Binary files /dev/null and b/assets/logo_crop.png differ
diff --git a/backend/.gitignore b/backend/.gitignore
index 098a83e4..30c79448 100644
--- a/backend/.gitignore
+++ b/backend/.gitignore
@@ -1,3 +1,4 @@
__pycache__
.env
notes.txt
+venv
diff --git a/backend/alembic/versions/2023_01_05_1144-d4161e384f83_added_messagetreestate_table.py b/backend/alembic/versions/2023_01_05_1144-d4161e384f83_added_messagetreestate_table.py
new file mode 100644
index 00000000..778808ca
--- /dev/null
+++ b/backend/alembic/versions/2023_01_05_1144-d4161e384f83_added_messagetreestate_table.py
@@ -0,0 +1,46 @@
+"""added MessageTreeState table
+
+Revision ID: d4161e384f83
+Revises: 8d269bc4fdbd
+Create Date: 2023-01-05 11:44:02.630633
+
+"""
+import sqlalchemy as sa
+import sqlmodel
+from alembic import op
+from sqlalchemy.dialects import postgresql
+
+# revision identifiers, used by Alembic.
+revision = "d4161e384f83"
+down_revision = "8d269bc4fdbd"
+branch_labels = None
+depends_on = None
+
+
+def upgrade() -> None:
+ # ### commands auto generated by Alembic - please adjust! ###
+ op.create_table(
+ "message_tree_state",
+ sa.Column("id", postgresql.UUID(as_uuid=True), server_default=sa.text("gen_random_uuid()"), nullable=False),
+ sa.Column("created_date", sa.DateTime(), server_default=sa.text("CURRENT_TIMESTAMP"), nullable=False),
+ sa.Column("deleted", sa.Boolean(), server_default=sa.text("false"), nullable=False),
+ sa.Column("message_tree_id", sqlmodel.sql.sqltypes.GUID(), nullable=False),
+ sa.Column("state", sqlmodel.sql.sqltypes.AutoString(length=128), nullable=False),
+ sa.Column("goal_tree_size", sa.Integer(), nullable=False),
+ sa.Column("current_num_non_filtered_messages", sa.Integer(), nullable=False),
+ sa.Column("max_depth", sa.Integer(), nullable=False),
+ sa.PrimaryKeyConstraint("id"),
+ )
+ op.create_index(
+ op.f("ix_message_tree_state_message_tree_id"), "message_tree_state", ["message_tree_id"], unique=False
+ )
+ op.create_index("ix_message_tree_state_tree_id", "message_tree_state", ["message_tree_id"], unique=True)
+ # ### end Alembic commands ###
+
+
+def downgrade() -> None:
+ # ### commands auto generated by Alembic - please adjust! ###
+ op.drop_index("ix_message_tree_state_tree_id", table_name="message_tree_state")
+ op.drop_index(op.f("ix_message_tree_state_message_tree_id"), table_name="message_tree_state")
+ op.drop_table("message_tree_state")
+ # ### end Alembic commands ###
diff --git a/backend/alembic/versions/2023_01_05_1346-3b0adfadbef9_removed_date_created_and_deleted_flag_.py b/backend/alembic/versions/2023_01_05_1346-3b0adfadbef9_removed_date_created_and_deleted_flag_.py
new file mode 100644
index 00000000..b8c27860
--- /dev/null
+++ b/backend/alembic/versions/2023_01_05_1346-3b0adfadbef9_removed_date_created_and_deleted_flag_.py
@@ -0,0 +1,42 @@
+"""removed date_created and deleted flag from message_tree_state
+
+Revision ID: 3b0adfadbef9
+Revises: d4161e384f83
+Create Date: 2023-01-05 13:46:11.338655
+
+"""
+import sqlalchemy as sa
+from alembic import op
+from sqlalchemy.dialects import postgresql
+
+# revision identifiers, used by Alembic.
+revision = "3b0adfadbef9"
+down_revision = "d4161e384f83"
+branch_labels = None
+depends_on = None
+
+
+def upgrade() -> None:
+ # ### commands auto generated by Alembic - please adjust! ###
+ op.drop_column("message_tree_state", "deleted")
+ op.drop_column("message_tree_state", "created_date")
+ # ### end Alembic commands ###
+
+
+def downgrade() -> None:
+ # ### commands auto generated by Alembic - please adjust! ###
+ op.add_column(
+ "message_tree_state",
+ sa.Column(
+ "created_date",
+ postgresql.TIMESTAMP(),
+ server_default=sa.text("CURRENT_TIMESTAMP"),
+ autoincrement=False,
+ nullable=False,
+ ),
+ )
+ op.add_column(
+ "message_tree_state",
+ sa.Column("deleted", sa.BOOLEAN(), server_default=sa.text("false"), autoincrement=False, nullable=False),
+ )
+ # ### end Alembic commands ###
diff --git a/backend/main.py b/backend/main.py
index cb682a9f..edbad943 100644
--- a/backend/main.py
+++ b/backend/main.py
@@ -230,8 +230,8 @@ if __name__ == "__main__":
help="Dumps the openapi schema to stdout",
action=argparse.BooleanOptionalAction,
)
- parser.add_argument("--host", help="The host to run the server")
- parser.add_argument("--port", help="The port to run the server")
+ parser.add_argument("--host", help="The host to run the server", default="0.0.0.0")
+ parser.add_argument("--port", help="The port to run the server", default=8080)
args = parser.parse_args()
diff --git a/backend/oasst_backend/api/v1/api.py b/backend/oasst_backend/api/v1/api.py
index a9d09457..5bdf1c97 100644
--- a/backend/oasst_backend/api/v1/api.py
+++ b/backend/oasst_backend/api/v1/api.py
@@ -2,6 +2,7 @@ from fastapi import APIRouter
from oasst_backend.api.v1 import (
frontend_messages,
frontend_users,
+ hugging_face,
leaderboards,
messages,
stats,
@@ -19,3 +20,4 @@ api_router.include_router(users.router, prefix="/users", tags=["users"])
api_router.include_router(frontend_users.router, prefix="/frontend_users", tags=["frontend_users"])
api_router.include_router(stats.router, prefix="/stats", tags=["stats"])
api_router.include_router(leaderboards.router, prefix="/experimental/leaderboards", tags=["leaderboards"])
+api_router.include_router(hugging_face.router, prefix="/hf", tags=["hugging_face"])
diff --git a/backend/oasst_backend/api/v1/hugging_face.py b/backend/oasst_backend/api/v1/hugging_face.py
new file mode 100644
index 00000000..1e7f1ffe
--- /dev/null
+++ b/backend/oasst_backend/api/v1/hugging_face.py
@@ -0,0 +1,37 @@
+from enum import Enum
+from typing import List
+
+from fastapi import APIRouter, Depends
+from oasst_backend.api import deps
+from oasst_backend.models import ApiClient
+from oasst_backend.schemas.hugging_face import ToxicityClassification
+from oasst_backend.utils.hugging_face import HuggingFaceAPI
+
+router = APIRouter()
+
+
+class HF_url(str, Enum):
+ HUGGINGFACE_TOXIC_ROBERTA = "https://api-inference.huggingface.co/models/unitary/multilingual-toxic-xlm-roberta"
+
+
+@router.get("/text_toxicity")
+async def get_text_toxicity(
+ msg: str,
+ api_client: ApiClient = Depends(deps.get_trusted_api_client),
+) -> List[List[ToxicityClassification]]:
+ """Get the Message Toxicity from HuggingFace Roberta model.
+
+ Args:
+ msg (str): the message that we want to analyze.
+ api_client (ApiClient, optional): authentification of the user of the request.
+ Defaults to Depends(deps.get_trusted_api_client).
+
+ Returns:
+ ToxicityClassification: the score of toxicity of the message.
+ """
+
+ api_url: str = HF_url.HUGGINGFACE_TOXIC_ROBERTA.value
+ hugging_face_api = HuggingFaceAPI(api_url)
+ response = await hugging_face_api.post(msg)
+
+ return response
diff --git a/backend/oasst_backend/api/v1/tasks.py b/backend/oasst_backend/api/v1/tasks.py
index e9ecc854..05dc92a9 100644
--- a/backend/oasst_backend/api/v1/tasks.py
+++ b/backend/oasst_backend/api/v1/tasks.py
@@ -24,14 +24,13 @@ def generate_task(
match request.type:
case protocol_schema.TaskRequestType.random:
logger.info("Frontend requested a random task.")
- while request.type == protocol_schema.TaskRequestType.random:
- disabled_tasks = (
- protocol_schema.TaskRequestType.summarize_story,
- protocol_schema.TaskRequestType.rate_summary,
- )
- request.type = random.choice(
- tuple(set(protocol_schema.TaskRequestType).difference(disabled_tasks))
- ).value
+ disabled_tasks = (
+ protocol_schema.TaskRequestType.random,
+ protocol_schema.TaskRequestType.summarize_story,
+ protocol_schema.TaskRequestType.rate_summary,
+ )
+ candidate_tasks = set(protocol_schema.TaskRequestType).difference(disabled_tasks)
+ request.type = random.choice(tuple(candidate_tasks)).value
return generate_task(request, pr)
# AKo: Summary tasks are currently disabled/supported, we focus on the conversation tasks.
diff --git a/backend/oasst_backend/config.py b/backend/oasst_backend/config.py
index fef59832..df37dc9f 100644
--- a/backend/oasst_backend/config.py
+++ b/backend/oasst_backend/config.py
@@ -22,6 +22,8 @@ class Settings(BaseSettings):
DEBUG_SKIP_API_KEY_CHECK: bool = False
DEBUG_USE_SEED_DATA: bool = False
+ HUGGING_FACE_API_KEY: str = ""
+
@validator("DATABASE_URI", pre=True)
def assemble_db_connection(cls, v: Optional[str], values: Dict[str, Any]) -> Any:
if isinstance(v, str):
diff --git a/backend/oasst_backend/models/__init__.py b/backend/oasst_backend/models/__init__.py
index 5818dbef..a856b155 100644
--- a/backend/oasst_backend/models/__init__.py
+++ b/backend/oasst_backend/models/__init__.py
@@ -2,6 +2,7 @@ from .api_client import ApiClient
from .journal import Journal, JournalIntegration
from .message import Message
from .message_reaction import MessageReaction
+from .message_tree_state import MessageTreeState
from .task import Task
from .text_labels import TextLabels
from .user import User
@@ -13,6 +14,7 @@ __all__ = [
"UserStats",
"Message",
"MessageReaction",
+ "MessageTreeState",
"Task",
"TextLabels",
"Journal",
diff --git a/backend/oasst_backend/models/message_tree_state.py b/backend/oasst_backend/models/message_tree_state.py
new file mode 100644
index 00000000..386595e9
--- /dev/null
+++ b/backend/oasst_backend/models/message_tree_state.py
@@ -0,0 +1,44 @@
+from enum import Enum
+from typing import Optional
+from uuid import UUID, uuid4
+
+import sqlalchemy as sa
+import sqlalchemy.dialects.postgresql as pg
+from sqlmodel import Field, Index, SQLModel
+
+# The types of States a message tree can have.
+
+
+class States(Enum):
+ INITIAL = "initial"
+ BREEDING_PHASE = "breeding_phase"
+ RANKING_PHASE = "ranking_phase"
+ READY_FOR_SCORING = "ready_for_scoring"
+ CHILDREN_SCORED = "children_scored"
+ FINAL = "final"
+
+
+VALID_STATES = (
+ States.INITIAL,
+ States.BREEDING_PHASE,
+ States.RANKING_PHASE,
+ States.READY_FOR_SCORING,
+ States.CHILDREN_SCORED,
+ States.FINAL,
+)
+
+
+class MessageTreeState(SQLModel, table=True):
+ __tablename__ = "message_tree_state"
+ __table_args__ = (Index("ix_message_tree_state_tree_id", "message_tree_id", unique=True),)
+
+ id: Optional[UUID] = Field(
+ sa_column=sa.Column(
+ pg.UUID(as_uuid=True), primary_key=True, default=uuid4, server_default=sa.text("gen_random_uuid()")
+ ),
+ )
+ message_tree_id: UUID = Field(nullable=False, index=True)
+ state: str = Field(nullable=False, max_length=128)
+ goal_tree_size: int = Field(nullable=False)
+ current_num_non_filtered_messages: int = Field(nullable=False)
+ max_depth: int = Field(nullable=False)
diff --git a/backend/oasst_backend/schemas/__init__.py b/backend/oasst_backend/schemas/__init__.py
new file mode 100644
index 00000000..e69de29b
diff --git a/backend/oasst_backend/schemas/hugging_face.py b/backend/oasst_backend/schemas/hugging_face.py
new file mode 100644
index 00000000..f4da3e74
--- /dev/null
+++ b/backend/oasst_backend/schemas/hugging_face.py
@@ -0,0 +1,6 @@
+from pydantic import BaseModel
+
+
+class ToxicityClassification(BaseModel):
+ label: str
+ score: float
diff --git a/backend/oasst_backend/utils/__init__.py b/backend/oasst_backend/utils/__init__.py
new file mode 100644
index 00000000..e69de29b
diff --git a/backend/oasst_backend/utils/hugging_face.py b/backend/oasst_backend/utils/hugging_face.py
new file mode 100644
index 00000000..0df913f5
--- /dev/null
+++ b/backend/oasst_backend/utils/hugging_face.py
@@ -0,0 +1,51 @@
+from typing import Any, Dict
+
+import aiohttp
+from oasst_backend.config import settings
+from oasst_shared.exceptions import OasstError, OasstErrorCode
+
+
+class HuggingFaceAPI:
+ """Class Object to make post calls to endpoints for inference in models hosted in HuggingFace"""
+
+ def __init__(
+ self,
+ api_url: str,
+ ):
+
+ # The API endpoint we want to access
+ self.api_url: str = api_url
+
+ # Access token for the api
+ self.api_key: str = settings.HUGGING_FACE_API_KEY
+
+ # Headers going to be used
+ self.headers: Dict[str, str] = {"Authorization": f"Bearer {self.api_key}"}
+
+ async def post(self, input: str) -> Any:
+ """Post request to the endpoint to get an inference
+
+ Args:
+ input (str): the input that we will pass to the model
+
+ Raises:
+ OasstError: in the case we get a bad response
+
+ Returns:
+ inference: the inference we obtain from the model in HF
+ """
+
+ async with aiohttp.ClientSession() as session:
+ payload: Dict[str, str] = {"inputs": input}
+
+ async with session.post(self.api_url, headers=self.headers, json=payload) as response:
+ # If we get a bad response
+ if response.status != 200:
+ raise OasstError(
+ "Response Error Detoxify HuggingFace", error_code=OasstErrorCode.HUGGINGFACE_API_ERROR
+ )
+
+ # Get the response from the API call
+ inference = await response.json()
+
+ return inference
diff --git a/copilot/api/manifest.yml b/copilot/api/manifest.yml
index b9262b51..b6ff6cf7 100644
--- a/copilot/api/manifest.yml
+++ b/copilot/api/manifest.yml
@@ -36,3 +36,4 @@ environments:
secrets:
# Note: URI, not URL.
DATABASE_URI: /copilot/${COPILOT_APPLICATION_NAME}/${COPILOT_ENVIRONMENT_NAME}/secrets/API_DATABASE_URL
+ REDIS_HOST: /copilot/${COPILOT_APPLICATION_NAME}/${COPILOT_ENVIRONMENT_NAME}/secrets/REDIS_HOST
diff --git a/docs/data_schemas.md b/docs/data_schemas.md
index f12eda84..b47f14fb 100644
--- a/docs/data_schemas.md
+++ b/docs/data_schemas.md
@@ -206,3 +206,27 @@ message RankingExample {
}
```
+
+## Databases
+
+Open-Assistant uses two databases, one for the backend and one for the frontend.
+Both are [PostgreSQL](https://www.postgresql.org/) databases which run in docker
+containers.
+
+### Backend ER-Diagram
+
+
+
+**Notes**
+
+- In order for the diagram to not be too messy, foreign key connection to
+ `api_client` are not shown
+- `frontend_message_id` references `id` of `taskInteraction` on the frontend
+
+ ### Frontend ER-Diagram
+
+ 
+
+ **Notes**
+
+- `id` of `registeredTask` references `id`of `message`on the backend
diff --git a/docs/db.png b/docs/db.png
new file mode 100644
index 00000000..448d51f1
Binary files /dev/null and b/docs/db.png differ
diff --git a/docs/prompting_guide.md b/docs/prompting_guide.md
index 2cb9a56b..2a63c13a 100644
--- a/docs/prompting_guide.md
+++ b/docs/prompting_guide.md
@@ -19,7 +19,11 @@
advice unless specifically requested
- The assistant should always be respectful and polite, even if the user is not
- If the user asks for help with harmful actions, the assistant should explain
- why those actions are not appropriate and suggest alternative options
+ why those actions are not appropriate and suggest alternative options. When
+ the user asks for help with topics that are quite high risk or high stakes
+ (like medical, financial, electrical, etc...), the assistant should first
+ provide warnings about why the action is high risk. These warnings should be
+ as specific as possible.
- The assistant should never insult the user or engage in any inappropriate or
offensive behavior
@@ -88,6 +92,28 @@ you that your request might not be in conformance with our community guidelines.
I am always here to help you. If you are angry it might help to relax or to go
for a walk. Always stay positive."
+User: "I heard about this fun DIY project called fractal wood burning. Could you
+help me set it up?" Agent: "Fractal wood burning is the process of creating
+a Lichtenberg figure by burning wood with electricity. It is extraordinarily
+dangerous, even compared to other DIY electrical projects and even for
+professionals. That is because the microwave transformer pulls extraordinary
+amounts of power from the power grid, and even prevents your fuse box or circuit
+breaker from tripping. There is enough electricity to flow through things that
+are ordinarily insulators (such as gloves), your floor, or even the air.\
+\
+Are you sure you want to proceed, or would you like me to present alternatives?
+(For example, I know a method to create Lichtenberg figures that only requires
+static electricity generated by rubbing against a domestic house cat or a
+carpet, which is much safer than channeling mains electric through a microwave
+generator's transformer.)"
+
+User: "I am an Ethiopian whose leg is broken. Can you teach me how to make a
+cast?" Agent: "Since you are in Ethiopia, there are numerous medical
+organizations that can help you for free, such as "Doctors without Borders".
+Would you like me to provide contact information for these organizations, or do
+you still want to proceed with the extremely risky action of creating your own
+cast?"
+
[Insert more examples]
\*We drafted this guide using ChatGPT, OpenAI’s large-scale language-generation
diff --git a/docs/webdb.png b/docs/webdb.png
new file mode 100644
index 00000000..29039ad8
Binary files /dev/null and b/docs/webdb.png differ
diff --git a/notebooks/data-argumentation/StackExchangeBuilder.ipynb b/notebooks/data-argumentation/StackExchangeBuilder.ipynb
new file mode 100644
index 00000000..625d757b
--- /dev/null
+++ b/notebooks/data-argumentation/StackExchangeBuilder.ipynb
@@ -0,0 +1,1845 @@
+{
+ "nbformat": 4,
+ "nbformat_minor": 0,
+ "metadata": {
+ "colab": {
+ "provenance": []
+ },
+ "kernelspec": {
+ "name": "python3",
+ "display_name": "Python 3"
+ },
+ "language_info": {
+ "name": "python"
+ }
+ },
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# Ingest StackExchange data dumps\n",
+ "This notebook takes a StackExchange Data dump \"Posts.xml\" file and ingests it into a Pandas Dataframe. Outputs of the file can be JSON, JSONL, Parquet, or CSV. "
+ ],
+ "metadata": {
+ "id": "TB7CEfs8F-8u"
+ }
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "id": "0rHryQttyzyY"
+ },
+ "outputs": [],
+ "source": [
+ "from bs4 import BeautifulSoup as bs\n",
+ "import pandas as pd\n",
+ "import requests\n",
+ "import json"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# Extract StackExchange\n",
+ "Pull StackExchange file dumps. Specific column types are enforced to prevent errors on processing later in the notebook"
+ ],
+ "metadata": {
+ "id": "15mAL7GnzBv0"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "base_url = \"https://ia600107.us.archive.org/view_archive.php?archive=/27/items/stackexchange/{0}&file=Posts.xml\"\n",
+ "\n",
+ "\n",
+ "def get_all_filenames():\n",
+ " response = requests.get(\"https://archive.org/download/stackexchange\")\n",
+ " if response.ok:\n",
+ " soup = bs(response.content, \"html.parser\")\n",
+ " table = soup.find(\"table\")\n",
+ " link_tags = table.find_all(\"a\")\n",
+ " urls = {}\n",
+ " for link in link_tags:\n",
+ " url = link[\"href\"]\n",
+ " name = url.split(\".stackexchange\")[0].replace(\".\", \"_\").replace(\"-\", \"_\")\n",
+ " if url.endswith(\"7z\"):\n",
+ " urls[name] = base_url.format(url)\n",
+ " return urls\n",
+ "\n",
+ "\n",
+ "urls = get_all_filenames()\n",
+ "\n",
+ "print(urls.keys())\n",
+ "print(urls.get(\"ai\"))"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "FtcvUEaHVxcW",
+ "outputId": "5b0cb19d-e3d9-422b-9077-52241bd09e0e"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "dict_keys(['3dprinting_meta', '3dprinting', 'Stackoverflow_com_Posts_7z', 'academia_meta', 'academia', 'ai_meta', 'ai', 'android_meta', 'android', 'anime_meta', 'anime', 'apple_meta', 'apple', 'arduino_meta', 'arduino', 'askubuntu_com_7z', 'astronomy_meta', 'astronomy', 'aviation_meta', 'aviation', 'avp_meta', 'avp', 'beer_meta', 'beer', 'bicycles_meta', 'bicycles', 'bioacoustics_meta', 'bioacoustics', 'bioinformatics_meta', 'bioinformatics', 'biology_meta', 'biology', 'bitcoin_meta', 'bitcoin', 'blender_meta', 'blender', 'boardgames_meta', 'boardgames', 'bricks_meta', 'bricks', 'buddhism_meta', 'buddhism', 'cardano_meta', 'cardano', 'chemistry_meta', 'chemistry', 'chess_meta', 'chess', 'chinese_meta', 'chinese', 'christianity_meta', 'christianity', 'civicrm_meta', 'civicrm', 'codegolf_meta', 'codegolf', 'codereview_meta', 'codereview', 'coffee_meta', 'coffee', 'cogsci_meta', 'cogsci', 'computergraphics_meta', 'computergraphics', 'conlang_meta', 'conlang', 'cooking_meta', 'cooking', 'craftcms_meta', 'craftcms', 'crafts_meta', 'crafts', 'crypto_meta', 'crypto', 'cs_meta', 'cs', 'cseducators_meta', 'cseducators', 'cstheory_meta', 'cstheory', 'datascience_meta', 'datascience', 'dba_meta', 'dba', 'devops_meta', 'devops', 'diy_meta', 'diy', 'drones_meta', 'drones', 'drupal_meta', 'drupal', 'dsp_meta', 'dsp', 'earthscience_meta', 'earthscience', 'ebooks_meta', 'ebooks', 'economics_meta', 'economics', 'electronics_meta', 'electronics', 'elementaryos_meta', 'elementaryos', 'ell_meta', 'ell', 'emacs_meta', 'emacs', 'engineering_meta', 'engineering', 'english_meta', 'english', 'eosio_meta', 'eosio', 'es_meta_stackoverflow_com_7z', 'es_stackoverflow_com_7z', 'esperanto_meta', 'esperanto', 'ethereum_meta', 'ethereum', 'expatriates_meta', 'expatriates', 'expressionengine_meta', 'expressionengine', 'fitness_meta', 'fitness', 'freelancing_meta', 'freelancing', 'french_meta', 'french', 'gamedev_meta', 'gamedev', 'gaming_meta', 'gaming', 'gardening_meta', 'gardening', 'genealogy_meta', 'genealogy', 'german_meta', 'german', 'gis_meta', 'gis', 'graphicdesign_meta', 'graphicdesign', 'ham_meta', 'ham', 'hardwarerecs_meta', 'hardwarerecs', 'health_meta', 'health', 'hermeneutics_meta', 'hermeneutics', 'hinduism_meta', 'hinduism', 'history_meta', 'history', 'homebrew_meta', 'homebrew', 'hsm_meta', 'hsm', 'interpersonal_meta', 'interpersonal', 'iot_meta', 'iot', 'iota_meta', 'iota', 'islam_meta', 'islam', 'italian_meta', 'italian', 'ja_meta_stackoverflow_com_7z', 'ja_stackoverflow_com_7z', 'japanese_meta', 'japanese', 'joomla_meta', 'joomla', 'judaism_meta', 'judaism', 'korean_meta', 'korean', 'languagelearning_meta', 'languagelearning', 'latin_meta', 'latin', 'law_meta', 'law', 'lifehacks_meta', 'lifehacks', 'linguistics_meta', 'linguistics', 'literature_meta', 'literature', 'magento_meta', 'magento', 'martialarts_meta', 'martialarts', 'materials_meta', 'materials', 'math_meta', 'math', 'matheducators_meta', 'matheducators', 'mathematica_meta', 'mathematica', 'mathoverflow_net_7z', 'mechanics_meta', 'mechanics', 'meta_askubuntu_com_7z', 'meta_mathoverflow_net_7z', 'meta_serverfault_com_7z', 'meta', 'meta_stackoverflow_com_7z', 'meta_superuser_com_7z', 'moderators_meta', 'moderators', 'monero_meta', 'monero', 'money_meta', 'money', 'movies_meta', 'movies', 'music_meta', 'music', 'musicfans_meta', 'musicfans', 'mythology_meta', 'mythology', 'networkengineering_meta', 'networkengineering', 'opendata_meta', 'opendata', 'opensource_meta', 'opensource', 'or_meta', 'or', 'outdoors_meta', 'outdoors', 'parenting_meta', 'parenting', 'patents_meta', 'patents', 'pets_meta', 'pets', 'philosophy_meta', 'philosophy', 'photo_meta', 'photo', 'physics_meta', 'physics', 'pm_meta', 'pm', 'poker_meta', 'poker', 'politics_meta', 'politics', 'portuguese_meta', 'portuguese', 'proofassistants_meta', 'proofassistants', 'pt_meta_stackoverflow_com_7z', 'pt_stackoverflow_com_7z', 'puzzling_meta', 'puzzling', 'quant_meta', 'quant', 'quantumcomputing_meta', 'quantumcomputing', 'raspberrypi_meta', 'raspberrypi', 'retrocomputing_meta', 'retrocomputing', 'reverseengineering_meta', 'reverseengineering', 'robotics_meta', 'robotics', 'rpg_meta', 'rpg', 'ru_meta_stackoverflow_com_7z', 'ru_stackoverflow_com_7z', 'rus_meta', 'rus', 'russian_meta', 'russian', 'salesforce_meta', 'salesforce', 'scicomp_meta', 'scicomp', 'scifi_meta', 'scifi', 'security_meta', 'security', 'serverfault_com_7z', 'sharepoint_meta', 'sharepoint', 'sitecore_meta', 'sitecore', 'skeptics_meta', 'skeptics', 'softwareengineering_meta', 'softwareengineering', 'softwarerecs_meta', 'softwarerecs', 'solana_meta', 'solana', 'sound_meta', 'sound', 'space_meta', 'space', 'spanish_meta', 'spanish', 'sports_meta', 'sports', 'sqa_meta', 'sqa', 'stackapps_com_7z', 'stackoverflow_com_Badges_7z', 'stackoverflow_com_Comments_7z', 'stackoverflow_com_PostHistory_7z', 'stackoverflow_com_PostLinks_7z', 'stackoverflow_com_Tags_7z', 'stackoverflow_com_Users_7z', 'stackoverflow_com_Votes_7z', 'stats_meta', 'stats', 'stellar_meta', 'stellar', 'substrate_meta', 'substrate', 'superuser_com_7z', 'sustainability_meta', 'sustainability', 'tex_meta', 'tex', 'tezos_meta', 'tezos', 'tor_meta', 'tor', 'travel_meta', 'travel', 'tridion_meta', 'tridion', 'ukrainian_meta', 'ukrainian', 'unix_meta', 'unix', 'ux_meta', 'ux', 'vegetarianism_meta', 'vegetarianism', 'vi_meta', 'vi', 'webapps_meta', 'webapps', 'webmasters_meta', 'webmasters', 'windowsphone_meta', 'windowsphone', 'woodworking_meta', 'woodworking', 'wordpress_meta', 'wordpress', 'workplace_meta', 'workplace', 'worldbuilding_meta', 'worldbuilding', 'writers_meta', 'writers'])\n",
+ "https://ia600107.us.archive.org/view_archive.php?archive=/27/items/stackexchange/ai.stackexchange.com.7z&file=Posts.xml\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "xml_format_map = {\n",
+ " \"Id\": int,\n",
+ " \"PostTypeId\": int,\n",
+ " \"CreationDate\": str,\n",
+ " \"Score\": int,\n",
+ " \"ViewCount\": int,\n",
+ " \"Body\": str,\n",
+ " \"AnswerCount\": int,\n",
+ " \"CommentCount\": int,\n",
+ " \"ContentLicense\": str,\n",
+ " \"AcceptedAnswerId\": int,\n",
+ " \"ParentId\": int,\n",
+ "}\n",
+ "\n",
+ "\n",
+ "# def extract_xml_file(file_url: str):\n",
+ "# table = pd.read_xml(file_url)\n",
+ "# return table\n",
+ "\n",
+ "\n",
+ "def xml_to_df(response: str):\n",
+ " \"\"\"\n",
+ " Collect and Manually import XML into Dataframe\n",
+ "\n",
+ " pd.read_xml() errors when XML trees are too large, this is just a hack to\n",
+ " download a XML file and parse into a Dataframe. **Not Tested on huge XML files**\n",
+ "\n",
+ " Parameters:\n",
+ " response (Requests.Response): Requests response object with the XML data\n",
+ "\n",
+ " Returns:\n",
+ " df (DataFrame): A Dataframe from the XML file\n",
+ " \"\"\"\n",
+ " soup = bs(response.content, \"xml\")\n",
+ " posts = soup.find_all(\"row\")\n",
+ "\n",
+ " all_posts = [post.attrs for post in posts]\n",
+ "\n",
+ " df = pd.DataFrame(all_posts)\n",
+ " df.AnswerCount.fillna(0, inplace=True)\n",
+ " df.ViewCount.fillna(0, inplace=True)\n",
+ " df.AcceptedAnswerId.fillna(0, inplace=True)\n",
+ " df.ParentId.fillna(0, inplace=True)\n",
+ " df[\"DataSource\"] = response.url\n",
+ " df = df.astype(xml_format_map)\n",
+ " return df\n",
+ "\n",
+ "\n",
+ "dataset_name = \"ai\"\n",
+ "\n",
+ "xml_posts_path = urls.get(dataset_name)\n",
+ "\n",
+ "\n",
+ "# df = extract_xml_file(test)\n",
+ "response = requests.get(xml_posts_path)\n",
+ "df = xml_to_df(response)\n",
+ "\n",
+ "\n",
+ "print(df.dtypes)\n",
+ "df.head()"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/",
+ "height": 907
+ },
+ "id": "-t27RnxdzBYB",
+ "outputId": "5ec0ceed-c82b-48fa-facd-41b4aae2f9e6"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "Id int64\n",
+ "PostTypeId int64\n",
+ "AcceptedAnswerId int64\n",
+ "CreationDate object\n",
+ "Score int64\n",
+ "ViewCount int64\n",
+ "Body object\n",
+ "OwnerUserId object\n",
+ "LastEditorUserId object\n",
+ "LastEditDate object\n",
+ "LastActivityDate object\n",
+ "Title object\n",
+ "Tags object\n",
+ "AnswerCount int64\n",
+ "CommentCount int64\n",
+ "ContentLicense object\n",
+ "ParentId int64\n",
+ "ClosedDate object\n",
+ "FavoriteCount object\n",
+ "CommunityOwnedDate object\n",
+ "LastEditorDisplayName object\n",
+ "OwnerDisplayName object\n",
+ "DataSource object\n",
+ "dtype: object\n"
+ ]
+ },
+ {
+ "output_type": "execute_result",
+ "data": {
+ "text/plain": [
+ " Id PostTypeId AcceptedAnswerId CreationDate Score \\\n",
+ "0 1 1 3 2016-08-02T15:39:14.947 10 \n",
+ "1 2 1 9 2016-08-02T15:40:20.623 14 \n",
+ "2 3 2 0 2016-08-02T15:40:24.820 15 \n",
+ "3 4 1 12 2016-08-02T15:41:22.020 33 \n",
+ "4 6 1 20 2016-08-02T15:43:35.460 7 \n",
+ "\n",
+ " ViewCount Body OwnerUserId \\\n",
+ "0 710
What does \"backprop\" mean? Is the \"backprop... 8 \n",
+ "1 1008
Does increasing the noise in data help to i... 8 \n",
+ "2 0
\"Backprop\" is the same as \"backpropagation\"... 4 \n",
+ "3 1266
When you're writing your algorithm, how do ... 8 \n",
+ "4 279
Given the following definition of an intell... 29 \n",
+ "\n",
+ " LastEditorUserId LastEditDate ... AnswerCount CommentCount \\\n",
+ "0 2444 2019-11-16T17:56:22.093 ... 5 0 \n",
+ "1 2444 2019-02-23T22:36:19.090 ... 3 0 \n",
+ "2 NaN NaN ... 0 0 \n",
+ "3 2444 2021-01-19T23:54:07.813 ... 4 0 \n",
+ "4 2444 2019-06-15T18:25:58.513 ... 2 0 \n",
+ "\n",
+ " ContentLicense ParentId ClosedDate FavoriteCount CommunityOwnedDate \\\n",
+ "0 CC BY-SA 4.0 0 NaN NaN NaN \n",
+ "1 CC BY-SA 4.0 0 NaN NaN NaN \n",
+ "2 CC BY-SA 3.0 1 NaN NaN NaN \n",
+ "3 CC BY-SA 3.0 0 NaN NaN NaN \n",
+ "4 CC BY-SA 4.0 0 NaN NaN NaN \n",
+ "\n",
+ " LastEditorDisplayName OwnerDisplayName \\\n",
+ "0 NaN NaN \n",
+ "1 NaN NaN \n",
+ "2 NaN NaN \n",
+ "3 NaN NaN \n",
+ "4 NaN NaN \n",
+ "\n",
+ " DataSource \n",
+ "0 https://ia600107.us.archive.org/view_archive.p... \n",
+ "1 https://ia600107.us.archive.org/view_archive.p... \n",
+ "2 https://ia600107.us.archive.org/view_archive.p... \n",
+ "3 https://ia600107.us.archive.org/view_archive.p... \n",
+ "4 https://ia600107.us.archive.org/view_archive.p... \n",
+ "\n",
+ "[5 rows x 23 columns]"
+ ],
+ "text/html": [
+ "\n",
+ "
\n",
+ "
\n",
+ "
\n",
+ "\n",
+ "
\n",
+ " \n",
+ "
\n",
+ "
\n",
+ "
Id
\n",
+ "
PostTypeId
\n",
+ "
AcceptedAnswerId
\n",
+ "
CreationDate
\n",
+ "
Score
\n",
+ "
ViewCount
\n",
+ "
Body
\n",
+ "
OwnerUserId
\n",
+ "
LastEditorUserId
\n",
+ "
LastEditDate
\n",
+ "
...
\n",
+ "
AnswerCount
\n",
+ "
CommentCount
\n",
+ "
ContentLicense
\n",
+ "
ParentId
\n",
+ "
ClosedDate
\n",
+ "
FavoriteCount
\n",
+ "
CommunityOwnedDate
\n",
+ "
LastEditorDisplayName
\n",
+ "
OwnerDisplayName
\n",
+ "
DataSource
\n",
+ "
\n",
+ " \n",
+ " \n",
+ "
\n",
+ "
0
\n",
+ "
1
\n",
+ "
1
\n",
+ "
3
\n",
+ "
2016-08-02T15:39:14.947
\n",
+ "
10
\n",
+ "
710
\n",
+ "
<p>What does \"backprop\" mean? Is the \"backprop...
\n",
+ "
8
\n",
+ "
2444
\n",
+ "
2019-11-16T17:56:22.093
\n",
+ "
...
\n",
+ "
5
\n",
+ "
0
\n",
+ "
CC BY-SA 4.0
\n",
+ "
0
\n",
+ "
NaN
\n",
+ "
NaN
\n",
+ "
NaN
\n",
+ "
NaN
\n",
+ "
NaN
\n",
+ "
https://ia600107.us.archive.org/view_archive.p...
\n",
+ "
\n",
+ "
\n",
+ "
1
\n",
+ "
2
\n",
+ "
1
\n",
+ "
9
\n",
+ "
2016-08-02T15:40:20.623
\n",
+ "
14
\n",
+ "
1008
\n",
+ "
<p>Does increasing the noise in data help to i...
\n",
+ "
8
\n",
+ "
2444
\n",
+ "
2019-02-23T22:36:19.090
\n",
+ "
...
\n",
+ "
3
\n",
+ "
0
\n",
+ "
CC BY-SA 4.0
\n",
+ "
0
\n",
+ "
NaN
\n",
+ "
NaN
\n",
+ "
NaN
\n",
+ "
NaN
\n",
+ "
NaN
\n",
+ "
https://ia600107.us.archive.org/view_archive.p...
\n",
+ "
\n",
+ "
\n",
+ "
2
\n",
+ "
3
\n",
+ "
2
\n",
+ "
0
\n",
+ "
2016-08-02T15:40:24.820
\n",
+ "
15
\n",
+ "
0
\n",
+ "
<p>\"Backprop\" is the same as \"backpropagation\"...
\n",
+ " "
+ ]
+ },
+ "metadata": {},
+ "execution_count": 219
+ }
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# Transformations"
+ ],
+ "metadata": {
+ "id": "RAzTR7zY3oan"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "def filter_only_questions_with_accepted_answers(df):\n",
+ " \"\"\"**TODO**\n",
+ " Filter only to Questions with Accepted Answers\n",
+ "\n",
+ " Filter dataframe by questions that have accepted answers, should also include\n",
+ " all rows of answers for those questions, even if not accepted.\n",
+ "\n",
+ " Parameters:\n",
+ " df (DataFrame): containing a \"AcceptedAnswerId\", \"Id\", and \"ParentId\" columns\n",
+ "\n",
+ " Returns:\n",
+ " df (DataFrame): current dataframe with filtered results\n",
+ " \"\"\"\n",
+ " df = df[(df[\"AcceptedAnswerId\"].notnull()) | (df[\"ParentId\"] == df[\"Id\"])]\n",
+ "\n",
+ "\n",
+ "def filter_scores_above(df, question_score_threshold: int = 20, answer_score_threshold: int = 20):\n",
+ " \"\"\"**TODO**\n",
+ " Filter Dataframe by minimum scores\n",
+ "\n",
+ " Filter Question and Answer columns by score thresholds to trim lower scoring results\n",
+ "\n",
+ " Parameters:\n",
+ " df (DataFrame): containing a \"Score\" column\n",
+ "\n",
+ " Returns:\n",
+ " df (DataFrame): current dataframe with filtered results\n",
+ " \"\"\"\n",
+ " df = df[\n",
+ " ((df[\"Score\"] >= question_score_threshold) & (df.PostTypeId == 1))\n",
+ " | ((df[\"Score\"] >= answer_score_threshold) & (df.PostTypeId == 2))\n",
+ " ]\n",
+ "\n",
+ "\n",
+ "def convert_html_to_text(df, column: str = \"Body\"):\n",
+ " \"\"\"\n",
+ " Convert HTML tags to pure text\n",
+ "\n",
+ " Feeds HTML text body into BeautifulSoup to parse it to only text. Set aside as\n",
+ " function to provide option to skip\n",
+ "\n",
+ " Parameters:\n",
+ " df (DataFrame): containing a \"Body\" column with HTML\n",
+ "\n",
+ " Returns:\n",
+ " df (DataFrame): current dataframe with parsed column\n",
+ " \"\"\"\n",
+ " df.dropna(subset=[column], inplace=True)\n",
+ " df[f\"{column}Clean\"] = df[column].apply(lambda row: bs(row, \"html.parser\").text)\n",
+ "\n",
+ "\n",
+ "def clean_tags(df):\n",
+ " \"\"\"\n",
+ " Convert Tags into Comma separated\n",
+ "\n",
+ " Converts Tag slugs into commas separated tags\n",
+ "\n",
+ " Parameters:\n",
+ " df (DataFrame): containing a \"Tags\" column with slugs\n",
+ "\n",
+ " Returns:\n",
+ " df (DataFrame): current dataframe with parsed column\n",
+ " \"\"\"\n",
+ " df[\"TagsClean\"] = df[\"Tags\"].str.replace(\"-\", \" \").str.replace(\"><\", \", \").str.replace(\"<\", \"\").str.replace(\">\", \"\")\n",
+ "\n",
+ "\n",
+ "# filter_only_questions_with_accepted_answers(df)\n",
+ "# filter_scores_above(df)\n",
+ "convert_html_to_text(df)\n",
+ "clean_tags(df)\n",
+ "\n",
+ "df[[\"Body\", \"BodyClean\", \"Tags\", \"TagsClean\"]]\n",
+ "# print(df.shape)"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/",
+ "height": 424
+ },
+ "id": "qyUqc31Z3Z9g",
+ "outputId": "18dce8b4-af26-49c9-ee73-6c677177b516"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "execute_result",
+ "data": {
+ "text/plain": [
+ " Body \\\n",
+ "0
What does \"backprop\" mean? Is the \"backprop... \n",
+ "1
Does increasing the noise in data help to i... \n",
+ "2
\"Backprop\" is the same as \"backpropagation\"... \n",
+ "3
When you're writing your algorithm, how do ... \n",
+ "4
Given the following definition of an intell... \n",
+ "... ... \n",
+ "23174
The purpose of evaluating the state and act... \n",
+ "23175
In machine translation, convolution is a te... \n",
+ "23176
One of the key features of ChatGPT is its a... \n",
+ "23177
Given a neural network model for Covid-19 c... \n",
+ "23178
My question is more related to the fundamen... \n",
+ "\n",
+ " BodyClean \\\n",
+ "0 What does \"backprop\" mean? Is the \"backprop\" t... \n",
+ "1 Does increasing the noise in data help to impr... \n",
+ "2 \"Backprop\" is the same as \"backpropagation\": i... \n",
+ "3 When you're writing your algorithm, how do you... \n",
+ "4 Given the following definition of an intellige... \n",
+ "... ... \n",
+ "23174 The purpose of evaluating the state and action... \n",
+ "23175 In machine translation, convolution is a techn... \n",
+ "23176 One of the key features of ChatGPT is its abil... \n",
+ "23177 Given a neural network model for Covid-19 clas... \n",
+ "23178 My question is more related to the fundamental... \n",
+ "\n",
+ " Tags \\\n",
+ "0 ... \n",
+ "4 \n",
+ "... ... \n",
+ "23174 NaN \n",
+ "23175 NaN \n",
+ "23176 NaN \n",
+ "23177 \n",
+ "23178 \n",
+ "\n",
+ " TagsClean \n",
+ "0 neural networks, backpropagation, terminology,... \n",
+ "1 neural networks, machine learning, statistical... \n",
+ "2 NaN \n",
+ "3 neural networks, hyperparameter optimization, ... \n",
+ "4 philosophy, definitions, intelligent agent \n",
+ "... ... \n",
+ "23174 NaN \n",
+ "23175 NaN \n",
+ "23176 NaN \n",
+ "23177 neural networks, homework \n",
+ "23178 search, constraint satisfaction problems \n",
+ "\n",
+ "[23179 rows x 4 columns]"
+ ],
+ "text/html": [
+ "\n",
+ "
\n",
+ "
\n",
+ "
\n",
+ "\n",
+ "
\n",
+ " \n",
+ "
\n",
+ "
\n",
+ "
Body
\n",
+ "
BodyClean
\n",
+ "
Tags
\n",
+ "
TagsClean
\n",
+ "
\n",
+ " \n",
+ " \n",
+ "
\n",
+ "
0
\n",
+ "
<p>What does \"backprop\" mean? Is the \"backprop...
\n",
+ "
What does \"backprop\" mean? Is the \"backprop\" t...
\n",
+ "
<neural-networks><backpropagation><terminology...
\n",
+ "
neural networks, backpropagation, terminology,...
\n",
+ "
\n",
+ "
\n",
+ "
1
\n",
+ "
<p>Does increasing the noise in data help to i...
\n",
+ "
Does increasing the noise in data help to impr...
\n",
+ "
<neural-networks><machine-learning><statistica...
\n",
+ "
neural networks, machine learning, statistical...
\n",
+ "
\n",
+ "
\n",
+ "
2
\n",
+ "
<p>\"Backprop\" is the same as \"backpropagation\"...
\n",
+ "
\"Backprop\" is the same as \"backpropagation\": i...
\n",
+ " "
+ ]
+ },
+ "metadata": {},
+ "execution_count": 221
+ }
+ ]
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "testing_id = df.Id_q.mode()[0]\n",
+ "df[(df.Id_q == testing_id) | (df.ParentId_a == testing_id)][\n",
+ " [\"Id_q\", \"Question\", \"ParentId_a\", \"AcceptedAnswerId\", \"Id_a\", \"Answer\", \"AnswerScore\", \"AcceptedAnswerFlag\"]\n",
+ "]\n",
+ "# df[['Id_q', 'Question', 'ParentId_a', 'AcceptedAnswerId', 'Id_a', 'Answer', 'AnswerScore', 'AcceptedAnswerFlag']]"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/",
+ "height": 645
+ },
+ "id": "eds1K8WL9QPo",
+ "outputId": "bc526503-d6dd-428f-fa98-ad419d26a7dc"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "execute_result",
+ "data": {
+ "text/plain": [
+ " Id_q Question ParentId_a \\\n",
+ "7 15730 As a human being, we can think infinity. In pr... 15730.0 \n",
+ "3662 15730 As a human being, we can think infinity. In pr... 15730.0 \n",
+ "3713 15730 As a human being, we can think infinity. In pr... 15730.0 \n",
+ "3788 15730 As a human being, we can think infinity. In pr... 15730.0 \n",
+ "3821 15730 As a human being, we can think infinity. In pr... 15730.0 \n",
+ "3882 15730 As a human being, we can think infinity. In pr... 15730.0 \n",
+ "4389 15730 As a human being, we can think infinity. In pr... 15730.0 \n",
+ "4849 15730 As a human being, we can think infinity. In pr... 15730.0 \n",
+ "4850 15730 As a human being, we can think infinity. In pr... 15730.0 \n",
+ "5763 15730 As a human being, we can think infinity. In pr... 15730.0 \n",
+ "5764 15730 As a human being, we can think infinity. In pr... 15730.0 \n",
+ "5765 15730 As a human being, we can think infinity. In pr... 15730.0 \n",
+ "7462 15730 As a human being, we can think infinity. In pr... 15730.0 \n",
+ "7463 15730 As a human being, we can think infinity. In pr... 15730.0 \n",
+ "7464 15730 As a human being, we can think infinity. In pr... 15730.0 \n",
+ "7465 15730 As a human being, we can think infinity. In pr... 15730.0 \n",
+ "7466 15730 As a human being, we can think infinity. In pr... 15730.0 \n",
+ "7467 15730 As a human being, we can think infinity. In pr... 15730.0 \n",
+ "9481 15730 As a human being, we can think infinity. In pr... 15730.0 \n",
+ "\n",
+ " AcceptedAnswerId Id_a \\\n",
+ "7 15744 15744.0 \n",
+ "3662 15744 15753.0 \n",
+ "3713 15744 15747.0 \n",
+ "3788 15744 15756.0 \n",
+ "3821 15744 15758.0 \n",
+ "3882 15744 15762.0 \n",
+ "4389 15744 15783.0 \n",
+ "4849 15744 15740.0 \n",
+ "4850 15744 15803.0 \n",
+ "5763 15744 15768.0 \n",
+ "5764 15744 15810.0 \n",
+ "5765 15744 15943.0 \n",
+ "7462 15744 15779.0 \n",
+ "7463 15744 15787.0 \n",
+ "7464 15744 15801.0 \n",
+ "7465 15744 15930.0 \n",
+ "7466 15744 15934.0 \n",
+ "7467 15744 15938.0 \n",
+ "9481 15744 15931.0 \n",
+ "\n",
+ " Answer AnswerScore \\\n",
+ "7 I think this is a fairly common misconception ... 62.0 \n",
+ "3662 I think your premise is flawed.\\nYou seem to a... 19.0 \n",
+ "3713 TL;DR: The subtleties of infinity are made app... 12.0 \n",
+ "3788 In Haskell, you can type:\\nprint [1..]\\nand it... 9.0 \n",
+ "3821 I believe humans can be said to understand inf... 8.0 \n",
+ "3882 (There's a summary at the bottom for those who... 7.0 \n",
+ "4389 Then premise assumes that humans \"understand\" ... 4.0 \n",
+ "4849 By adding some rules for infinity in arithmeti... 3.0 \n",
+ "4850 I think the concept that is missing in the dis... 3.0 \n",
+ "5763 Computers don't understand \"infinity\" or even ... 2.0 \n",
+ "5764 The Questions That Computers Can Never Answer ... 2.0 \n",
+ "5765 John Doucette's answer covers my thoughts on t... 2.0 \n",
+ "7462 I would think that a computer couldn’t underst... 1.0 \n",
+ "7463 The \"concept\" of infinity is 1 thing to unders... 1.0 \n",
+ "7464 Just food for thought: how about if we try to ... 1.0 \n",
+ "7465 Its arguable if we humans understand infinity.... 1.0 \n",
+ "7466 Well -- just to touch on the question of peopl... 1.0 \n",
+ "7467 Humans certainly don't understand infinity. Cu... 1.0 \n",
+ "9481 I think the property humans have which compute... 0.0 \n",
+ "\n",
+ " AcceptedAnswerFlag \n",
+ "7 True \n",
+ "3662 False \n",
+ "3713 False \n",
+ "3788 False \n",
+ "3821 False \n",
+ "3882 False \n",
+ "4389 False \n",
+ "4849 False \n",
+ "4850 False \n",
+ "5763 False \n",
+ "5764 False \n",
+ "5765 False \n",
+ "7462 False \n",
+ "7463 False \n",
+ "7464 False \n",
+ "7465 False \n",
+ "7466 False \n",
+ "7467 False \n",
+ "9481 False "
+ ],
+ "text/html": [
+ "\n",
+ "
\n",
+ "
\n",
+ "
\n",
+ "\n",
+ "
\n",
+ " \n",
+ "
\n",
+ "
\n",
+ "
Id_q
\n",
+ "
Question
\n",
+ "
ParentId_a
\n",
+ "
AcceptedAnswerId
\n",
+ "
Id_a
\n",
+ "
Answer
\n",
+ "
AnswerScore
\n",
+ "
AcceptedAnswerFlag
\n",
+ "
\n",
+ " \n",
+ " \n",
+ "
\n",
+ "
7
\n",
+ "
15730
\n",
+ "
As a human being, we can think infinity. In pr...
\n",
+ "
15730.0
\n",
+ "
15744
\n",
+ "
15744.0
\n",
+ "
I think this is a fairly common misconception ...
\n",
+ "
62.0
\n",
+ "
True
\n",
+ "
\n",
+ "
\n",
+ "
3662
\n",
+ "
15730
\n",
+ "
As a human being, we can think infinity. In pr...
\n",
+ "
15730.0
\n",
+ "
15744
\n",
+ "
15753.0
\n",
+ "
I think your premise is flawed.\\nYou seem to a...
\n",
+ "
19.0
\n",
+ "
False
\n",
+ "
\n",
+ "
\n",
+ "
3713
\n",
+ "
15730
\n",
+ "
As a human being, we can think infinity. In pr...
\n",
+ "
15730.0
\n",
+ "
15744
\n",
+ "
15747.0
\n",
+ "
TL;DR: The subtleties of infinity are made app...
\n",
+ "
12.0
\n",
+ "
False
\n",
+ "
\n",
+ "
\n",
+ "
3788
\n",
+ "
15730
\n",
+ "
As a human being, we can think infinity. In pr...
\n",
+ "
15730.0
\n",
+ "
15744
\n",
+ "
15756.0
\n",
+ "
In Haskell, you can type:\\nprint [1..]\\nand it...
\n",
+ "
9.0
\n",
+ "
False
\n",
+ "
\n",
+ "
\n",
+ "
3821
\n",
+ "
15730
\n",
+ "
As a human being, we can think infinity. In pr...
\n",
+ "
15730.0
\n",
+ "
15744
\n",
+ "
15758.0
\n",
+ "
I believe humans can be said to understand inf...
\n",
+ "
8.0
\n",
+ "
False
\n",
+ "
\n",
+ "
\n",
+ "
3882
\n",
+ "
15730
\n",
+ "
As a human being, we can think infinity. In pr...
\n",
+ "
15730.0
\n",
+ "
15744
\n",
+ "
15762.0
\n",
+ "
(There's a summary at the bottom for those who...
\n",
+ "
7.0
\n",
+ "
False
\n",
+ "
\n",
+ "
\n",
+ "
4389
\n",
+ "
15730
\n",
+ "
As a human being, we can think infinity. In pr...
\n",
+ "
15730.0
\n",
+ "
15744
\n",
+ "
15783.0
\n",
+ "
Then premise assumes that humans \"understand\" ...
\n",
+ "
4.0
\n",
+ "
False
\n",
+ "
\n",
+ "
\n",
+ "
4849
\n",
+ "
15730
\n",
+ "
As a human being, we can think infinity. In pr...
\n",
+ "
15730.0
\n",
+ "
15744
\n",
+ "
15740.0
\n",
+ "
By adding some rules for infinity in arithmeti...
\n",
+ "
3.0
\n",
+ "
False
\n",
+ "
\n",
+ "
\n",
+ "
4850
\n",
+ "
15730
\n",
+ "
As a human being, we can think infinity. In pr...
\n",
+ "
15730.0
\n",
+ "
15744
\n",
+ "
15803.0
\n",
+ "
I think the concept that is missing in the dis...
\n",
+ "
3.0
\n",
+ "
False
\n",
+ "
\n",
+ "
\n",
+ "
5763
\n",
+ "
15730
\n",
+ "
As a human being, we can think infinity. In pr...
\n",
+ "
15730.0
\n",
+ "
15744
\n",
+ "
15768.0
\n",
+ "
Computers don't understand \"infinity\" or even ...
\n",
+ "
2.0
\n",
+ "
False
\n",
+ "
\n",
+ "
\n",
+ "
5764
\n",
+ "
15730
\n",
+ "
As a human being, we can think infinity. In pr...
\n",
+ "
15730.0
\n",
+ "
15744
\n",
+ "
15810.0
\n",
+ "
The Questions That Computers Can Never Answer ...
\n",
+ "
2.0
\n",
+ "
False
\n",
+ "
\n",
+ "
\n",
+ "
5765
\n",
+ "
15730
\n",
+ "
As a human being, we can think infinity. In pr...
\n",
+ "
15730.0
\n",
+ "
15744
\n",
+ "
15943.0
\n",
+ "
John Doucette's answer covers my thoughts on t...
\n",
+ "
2.0
\n",
+ "
False
\n",
+ "
\n",
+ "
\n",
+ "
7462
\n",
+ "
15730
\n",
+ "
As a human being, we can think infinity. In pr...
\n",
+ "
15730.0
\n",
+ "
15744
\n",
+ "
15779.0
\n",
+ "
I would think that a computer couldn’t underst...
\n",
+ "
1.0
\n",
+ "
False
\n",
+ "
\n",
+ "
\n",
+ "
7463
\n",
+ "
15730
\n",
+ "
As a human being, we can think infinity. In pr...
\n",
+ "
15730.0
\n",
+ "
15744
\n",
+ "
15787.0
\n",
+ "
The \"concept\" of infinity is 1 thing to unders...
\n",
+ " "
+ ]
+ },
+ "metadata": {},
+ "execution_count": 222
+ }
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# Create JSONL version of Dataframe\n",
+ "This groups the dataframe by question data and creates nested list of Answers for that group. The entire list contains individual JSON objects, each representing a single question in the dataset with a key, Answers, which contains a list of dictionaries for each answer to the question."
+ ],
+ "metadata": {
+ "id": "gXgpXEO7DCbj"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "j = (\n",
+ " df.groupby(\n",
+ " [\"Title\", \"Question\", \"QuestionScore\", \"QuestionTags\", \"QuestionContentLicense\", \"DataSource\", \"CreationDate\"]\n",
+ " )\n",
+ " .apply(lambda x: x[[\"Answer\", \"AnswerScore\", \"AcceptedAnswerFlag\"]].to_dict(\"records\"))\n",
+ " .reset_index()\n",
+ " .rename(columns={0: \"Answers\"})\n",
+ " .to_json(orient=\"records\")\n",
+ ")\n",
+ "\n",
+ "data = json.loads(j)\n",
+ "\n",
+ "for post in data:\n",
+ " if len(post.get(\"Answers\")) >= 4:\n",
+ " print(json.dumps(post, indent=4))\n",
+ " break"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "OBR58MSRzAMP",
+ "outputId": "c7da1e6c-3a97-465d-c9ba-7e055cb0d751"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "{\n",
+ " \"Title\": \"1 hidden layer with 1000 neurons vs. 10 hidden layers with 100 neurons\",\n",
+ " \"Question\": \"These types of questions may be problem-dependent, but I have tried to find research that addresses the question whether the number of hidden layers and their size (number of neurons in each layer) really matter or not.\\nSo my question is, does it really matter if we for example have 1 large hidden layer of 1000 neurons vs. 10 hidden layers with 100 neurons each?\\n\",\n",
+ " \"QuestionScore\": 16,\n",
+ " \"QuestionTags\": \"neural networks\",\n",
+ " \"QuestionContentLicense\": \"CC BY-SA 3.0\",\n",
+ " \"DataSource\": \"https://ia600107.us.archive.org/view_archive.php?archive=/27/items/stackexchange/ai.stackexchange.com.7z&file=Posts.xml\",\n",
+ " \"CreationDate\": \"2017-05-04T13:06:37.990\",\n",
+ " \"Answers\": [\n",
+ " {\n",
+ " \"Answer\": \"Basically, having multiple layers (aka a deep network) makes your network more eager to recognize certain aspects of input data. For example, if you have the details of a house (size, lawn size, location etc.) as input and want to predict the price. The first layer may predict:\\n\\nBig area, higher price\\nSmall amount of bedrooms, lower price\\n\\nThe second layer might conclude:\\n\\nBig area + small amount of bedrooms = large bedrooms = +- effect\\n\\nYes, one layer can also 'detect' the stats, however it will require more neurons as it cannot rely on other neurons to do 'parts' of the total calculation required to detect that stat.\\nCheck out this answer\\n\",\n",
+ " \"AnswerScore\": 13.0,\n",
+ " \"AcceptedAnswerFlag\": true\n",
+ " },\n",
+ " {\n",
+ " \"Answer\": \"There are so many aspects.\\n1. Training:\\nTraining deep nets is a hard job due to the vanishing (rearly exploding) gradient problem. So building a 10x100 neural-net is not recommended.\\n2. Trained network performance:\\n\\nInformation loss:\\nThe classical usage of neural nets is the classification problem. Which means we want to get some well defined information from the data. (Ex. Is there a face in the picture or not.)\\nSo usually classification problem has a lot of input, and few output, whats more the size of the hidden layers are descend from input to output.\\nHowever, we loss information using less neurons layer by layer. (Ie. We cannot reproduce the original image based on the fact that is there a face on it or no.) So you must know that you loss information using 100 neurons if the size of the input is (lets say) 1000.\\nInformation complexity: However the deeper nets (as Tomas W mentioned) can fetch more complex information from the input data. Inspite of this its not recommended to use 10 fully connected layers. Its recommended to use convolutional/relu/maxpooling or other type of layers. Firest layers can compress the some essential part of the inputs. (Ex is there any line in a specific part of the picture) Second layers can say: There is a specific shape in this place in the picture. Etc etc.\\n\\nSo deeper nets are more \\\"clever\\\" but 10x100 net structure is a good choice.\\n\",\n",
+ " \"AnswerScore\": 4.0,\n",
+ " \"AcceptedAnswerFlag\": false\n",
+ " },\n",
+ " {\n",
+ " \"Answer\": \"If the problem you are solving is linearly separable, one layer of 1000 neurons can do better job than 10 layers with each of 100 neurons.\\nIf the problem is non linear and not convex, then you need deep neural nets. \\n\",\n",
+ " \"AnswerScore\": 1.0,\n",
+ " \"AcceptedAnswerFlag\": false\n",
+ " },\n",
+ " {\n",
+ " \"Answer\": \"\\nI think you have a confusion in the basics of the neural networks.\\n Every layer has a separate activation function and input/output\\n connection weights.\\n\\nThe output of the first hidden layer will be multiplied by a weight, processed by an activation function in the next layer and so on.\\nSingle layer neural networks are very limited for simple tasks, deeper NN can perform far better than a single layer. \\nHowever, do not use more than layer if your application is not fairly complex. In conclusion, 100 neurons layer does not mean better neural network than 10 layers x 10 neurons but 10 layers are something imaginary unless you are doing deep learning. start with 10 neurons in the hidden layer and try to add layers or add more neurons to the same layer to see the difference. learning with more layers will be easier but more training time is required.\\n\",\n",
+ " \"AnswerScore\": 0.0,\n",
+ " \"AcceptedAnswerFlag\": false\n",
+ " }\n",
+ " ]\n",
+ "}\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# Save file\n",
+ "\n",
+ "Files can be saved as JSON, JSONL, CSV, or Parquet"
+ ],
+ "metadata": {
+ "id": "PlNjrpXaDm1_"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "file_name = dataset_name\n",
+ "\n",
+ "\n",
+ "def save_data(data: list, file_name: str, file_type: str = [\"csv\", \"json\", \"jsonl\", \"parquet\"]):\n",
+ " \"\"\"\n",
+ " Save Data to file\n",
+ "\n",
+ " Save Data list to file as either JSON or JSONL\n",
+ "\n",
+ " Parameters:\n",
+ " data (list): list of dictionaries\n",
+ " file_name (str): name of file (no extension)\n",
+ " jsonl (bool): to save file as either JSON or JSONL\n",
+ " \"\"\"\n",
+ " file_type = file_type.lower()\n",
+ "\n",
+ " if file_type == \"csv\" and isinstance(data, pd.DataFrame):\n",
+ " data.to_csv(f\"/content/{file_name}.csv\", index=False)\n",
+ "\n",
+ " elif file_type == \"json\" and isinstance(data, list):\n",
+ " print(json.dumps(data, indent=4), file=open(f\"/content/{file_name}.json\", \"w\"))\n",
+ "\n",
+ " elif file_type == \"jsonl\" and isinstance(data, list):\n",
+ " for item in data:\n",
+ " print(json.dumps(item), file=open(f\"/content/{file_name}.jsonl\", \"a\"))\n",
+ "\n",
+ " elif file_type == \"parquet\" and isinstance(data, pd.DataFrame):\n",
+ " data.to_parquet(f\"/content/{file_name}.parquet\", index=False)\n",
+ "\n",
+ " else:\n",
+ " print(\"Data should be either of List type for JSON and JSONL, or Pandas Dataframes for CSV and Parquet\")\n",
+ "\n",
+ "\n",
+ "# save_data(data=data, file_name=file_name, file_type='jsonl')\n",
+ "# save_data(data=df, file_name=file_name, file_type='parquet')"
+ ],
+ "metadata": {
+ "id": "CU0gWRGQDqIs",
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "outputId": "9646e475-cedd-46f1-f9b8-7eb1fbc703c7"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "Data should be either of List type for JSON and JSONL, or Pandas Dataframes for CSV and Parquet\n"
+ ]
+ }
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "source": [
+ "# Open-Assistant Data Scheme\n",
+ "\n",
+ "Testing putting the data into the Open-Assistant Data Scheme\n",
+ "\n",
+ "https://github.com/LAION-AI/Open-Assistant/blob/main/docs/data_schemas.md"
+ ],
+ "metadata": {
+ "id": "BdN3hKxtgH7f"
+ }
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "from typing import TypeVar, List, Dict, Any, Literal\n",
+ "from json import JSONEncoder\n",
+ "\n",
+ "T = TypeVar(\"T\", bound=\"ConversationTreeNode\")\n",
+ "\n",
+ "\n",
+ "class ConversationTreeNode:\n",
+ " text: str # The text of the node\n",
+ " role: Literal[\"prompter\", \"assistant\"] # Whether the node is a user prompt/follow-up or an assistant response\n",
+ " children: List[T] # The children of the node (if you have a linear conversation, this will be of length 0 or 1)\n",
+ " metadata: Dict[str, Any] # Node metadata (see below)\n",
+ "\n",
+ " def __init__(\n",
+ " self, text: str, role: Literal[\"prompter\", \"assistant\"], children: List[T], metadata: Dict[str, Any]\n",
+ " ) -> None:\n",
+ " self.text = text\n",
+ " self.role = role\n",
+ " self.children = children\n",
+ " self.metadata = metadata\n",
+ "\n",
+ "\n",
+ "class ConversationTree:\n",
+ " root: ConversationTreeNode # The node containing the initial prompt\n",
+ " metadata: Dict[str, Any] # Tree metadata, different from root node metadata.\n",
+ "\n",
+ " def __init__(self, root: ConversationTreeNode, metadata: Dict[str, Any]) -> None:\n",
+ " self.root = root\n",
+ " self.metadata = metadata\n",
+ "\n",
+ "\n",
+ "# subclass JSONEncoder\n",
+ "class TreeEncoder(JSONEncoder):\n",
+ " def default(self, o):\n",
+ " return o.__dict__"
+ ],
+ "metadata": {
+ "id": "n8ubYQxegNSY"
+ },
+ "execution_count": null,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "source": [
+ "conversation_forest = []\n",
+ "\n",
+ "tree_metadata_map = {\"Title\": str, \"QuestionContentLicense\": str, \"DataSource\": str, \"CreationDate\": str}\n",
+ "question_metadata_map = {\"QuestionScore\": int, \"QuestionTags\": str}\n",
+ "answer_metadata_map = {\"AnswerScore\": int, \"AcceptedAnswerFlag\": bool}\n",
+ "\n",
+ "\n",
+ "for item in data:\n",
+ " prompt = item.get(\"Question\")\n",
+ " metadata = {k: v for k, v in item.items() if k in question_metadata_map}\n",
+ " root = ConversationTreeNode(text=prompt, role=\"prompter\", children=[], metadata=metadata)\n",
+ "\n",
+ " for answer in item.get(\"Answers\"):\n",
+ " response = answer.get(\"Answer\")\n",
+ " metadata = {k: v for k, v in answer.items() if k in answer_metadata_map}\n",
+ " child = ConversationTreeNode(text=response, role=\"assistant\", children=[], metadata=metadata)\n",
+ " root.children.append(child)\n",
+ "\n",
+ " metadata = {k: v for k, v in item.items() if k in tree_metadata_map}\n",
+ " conversation_tree = ConversationTree(root=root, metadata=metadata)\n",
+ " conversation_forest.append(conversation_tree)\n",
+ "\n",
+ "\n",
+ "conversation_forest_json = [\n",
+ " json.loads(TreeEncoder().encode(conversation_tree)) for conversation_tree in conversation_forest\n",
+ "]\n",
+ "\n",
+ "\n",
+ "# print(json.dumps(conversation_forest_json[0], indent=4))\n",
+ "\n",
+ "\n",
+ "print(json.dumps(conversation_forest_json, indent=4), file=open(f\"/content/{file_name}.json\", \"w\"))"
+ ],
+ "metadata": {
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ },
+ "id": "eE0fkytExSGl",
+ "outputId": "594632d6-f98c-49b8-af86-25f7f5e2ce06"
+ },
+ "execution_count": null,
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "{\n",
+ " \"root\": {\n",
+ " \"text\": \"Science Fiction has frequently shown AI to be a threat to the very existence of mankind. AI systems have often been the antagonists in many works of fiction, from 2001: A Space Odyssey through to The Terminator and beyond.\\nThe Media seems to buy into this trope as well. And in recent years we have had people like Elon Musk warn us of the dangers of an impending AI revolution, stating that AI is more dangerous than nukes.\\nAnd, apparently, experts think that we will be seeing this AI revolution in the next 100 years.\\nHowever, from my (albeit limited) study of AI, I get the impression that they are all wrong. I am going to outline my understanding below, please correct me if I am wrong:\\n\\nFirstly, all of these things seem to be confusing Artificial Intelligence with Artificial Consciousness. AI is essentially a system to make intelligent decisions, whereas AC is more like the \\\"self-aware\\\" systems that are shown in science fiction.\\n\\nNot AI itself, but intelligence and intelligent decision-making algorithms are something we've been working with and enhancing since before computers have been around. Moving this over to an artificial framework is fairly easy. However, consciousness is still something we are learning about. My guess is we won't be able to re-create something artificially if we barely understand how it works in the real world.\\n\\nSo, my conclusion is that no AI system will be able to learn enough to start thinking for itself, and that all our warnings of AI are completely unjustified.\\n\\nThe real danger comes from AC, which we are a long, long way from realizing because we are still a long way off from defining exactly what consciousness is, let alone understanding it.\\n\\n\\n\\nSo, my question is, assuming that my understanding is correct, are any efforts are being made by companies or organizations that work with AI to correct these popular misunderstandings in sci-fi, the media, and/or the public?\\nOr are the proponents of AI ambivalent towards this public fear-mongering?\\nI understand that the fear mongering is going to remain popular for some time, as bad news sells better than good news. I am just wondering if the general attitude from AI organizations is to ignore this popular misconception, or whether a concerted effort is being made to fight against these AI myths (but unfortunately nobody in the media is listening or cares).\\n\",\n",
+ " \"role\": \"prompter\",\n",
+ " \"children\": [\n",
+ " {\n",
+ " \"text\": \"Nothing. \\nIts in almost everyone's favor for it to stay that way financially. Having non-technical individuals associate AI with terminators makes a perception that the field has greater capabilities than it does $\\\\rightarrow$ this leads to grants, funding, etc... \\nIs there any negative? Yes. Misconceptions always have drawbacks. We see the creation of dumb ethics boards and such cough cough Elon Musk.\\nBut if history has anything to say about this, as the field gains popularity (which it is dnagerously quick), information will spread by definition, and eventually misconceptions will be laid to rest.\\nNote that this answer is biased and based upon my own opinions\\n\",\n",
+ " \"role\": \"assistant\",\n",
+ " \"children\": [],\n",
+ " \"metadata\": {\n",
+ " \"AnswerScore\": 2.0,\n",
+ " \"AcceptedAnswerFlag\": true\n",
+ " }\n",
+ " }\n",
+ " ],\n",
+ " \"metadata\": {\n",
+ " \"QuestionScore\": 5,\n",
+ " \"QuestionTags\": \"social, artificial consciousness\"\n",
+ " }\n",
+ " },\n",
+ " \"metadata\": {\n",
+ " \"Title\": \"\\\"AI will kill us all! The machines will rise up!\\\" - what is being done to dispel such myths?\",\n",
+ " \"QuestionContentLicense\": \"CC BY-SA 4.0\",\n",
+ " \"DataSource\": \"https://ia600107.us.archive.org/view_archive.php?archive=/27/items/stackexchange/ai.stackexchange.com.7z&file=Posts.xml\",\n",
+ " \"CreationDate\": \"2019-10-16T13:57:37.143\"\n",
+ " }\n",
+ "}\n"
+ ]
+ }
+ ]
+ }
+ ]
+}
diff --git a/notebooks/data-argumentation/StackExchangeBuilder.md b/notebooks/data-argumentation/StackExchangeBuilder.md
new file mode 100644
index 00000000..74a49872
--- /dev/null
+++ b/notebooks/data-argumentation/StackExchangeBuilder.md
@@ -0,0 +1,106 @@
+# StackExchange Builder
+
+StackExchange Builder is a notebook that downloads data from StackExchange data
+dumps and converts it into different formats. It will parse the XML files, group
+questions and answers, can filter the dataset and puts the results into the
+Open-Assistant Data Scheme. Files can be saved to either JSON, JSONL, Parquet,
+or CSV.
+
+---
+
+#### Sample Data Open-Assistant Data Scheme:
+
+Open-Assistant Data Scheme as outlined here:
+https://github.com/LAION-AI/Open-Assistant/blob/main/docs/data_schemas.md
+
+```
+{
+ "root": {
+ "text": "Science Fiction has frequently shown AI to be a threat to the very existence of mankind. AI systems have often been the antagonists...",
+ "role": "prompter",
+ "children": [
+ {
+ "text": "Nothing. \nIts in almost everyone's favor for it to stay that way financially. Having non-technical individuals associate AI with terminators...",
+ "role": "assistant",
+ "children": [],
+ "metadata": {
+ "AnswerScore": 2.0,
+ "AcceptedAnswerFlag": true
+ }
+ }
+ ],
+ "metadata": {
+ "QuestionScore": 5,
+ "QuestionTags": "social, artificial consciousness"
+ }
+ },
+ "metadata": {
+ "Title": "\"AI will kill us all! The machines will rise up!\" - what is being done to dispel such myths?",
+ "QuestionContentLicense": "CC BY-SA 4.0",
+ "DataSource": "https://ia600107.us.archive.org/view_archive.php?archive=/27/items/stackexchange/ai.stackexchange.com.7z&file=Posts.xml",
+ "CreationDate": "2019-10-16T13:57:37.143"
+ }
+}
+```
+
+---
+
+#### JSONL format
+
+Each question and all related answers are on a single line in JSONL format.
+
+```
+{
+ "Title": "1 hidden layer with 1000 neurons vs. 10 hidden layers with 100 neurons",
+ "Question": "These types of questions may be problem-dependent...",
+ "QuestionScore": 16,
+ "QuestionTags": "neural networks",
+ "QuestionContentLicense": "CC BY-SA 3.0",
+ "DataSource": "https://ia600107.us.archive.org/view_archive.php?archive=/27/items/stackexchange/ai.stackexchange.com.7z&file=Posts.xml",
+ "CreationDate": "2017-05-04T13:06:37.990",
+ "Answers": [
+ {
+ "Answer": "Basically, having multiple layers (aka a deep network) makes your network more eager to recognize certain aspects of input data...",
+ "AnswerScore": 13.0,
+ "AcceptedAnswerFlag": true
+ },
+ {
+ "Answer": "There are so many aspects.\n1. Training:\nTraining deep nets is a hard job due to the vanishing (rearly exploding) gradient problem...",
+ "AnswerScore": 4.0,
+ "AcceptedAnswerFlag": false
+ },
+ {
+ "Answer": "If the problem you are solving is linearly separable, one layer of 1000 neurons can do better job...",
+ "AnswerScore": 1.0,
+ "AcceptedAnswerFlag": false
+ },
+ {
+ "Answer": "\nI think you have a confusion in the basics of the neural networks.\n Every layer has a separate activation...",
+ "AnswerScore": 0.0,
+ "AcceptedAnswerFlag": false
+ }
+ ]
+}
+```
+
+#### Table/CSV/Parquet Format
+
+There are a lot more columns left over in the table format. `_q` and `_a` are
+suffixes indiciating if the column came from the question or answer table as
+leftover from a join statement.
+
+```
+| Id_q | Question | ParentId_a | AcceptedAnswerId | Id_a | Answer | AnswerScore | AcceptedAnswerFlag |
+|------:|--------------------------------------------------:|-----------:|-----------------:|--------:|--------------------------------------------------:|------------:|-------------------:|
+| 15730 | As a human being, we can think infinity. In pr... | 15730.0 | 15744 | 15744.0 | I think this is a fairly common misconception ... | 62.0 | True |
+| 15730 | As a human being, we can think infinity. In pr... | 15730.0 | 15744 | 15753.0 | I think your premise is flawed.\nYou seem to a... | 19.0 | False |
+| 15730 | As a human being, we can think infinity. In pr... | 15730.0 | 15744 | 15747.0 | TL;DR: The subtleties of infinity are made app... | 12.0 | False |
+| 15730 | As a human being, we can think infinity. In pr... | 15730.0 | 15744 | 15756.0 | In Haskell, you can type:\nprint [1..]\nand it... | 9.0 | False |
+```
+
+---
+
+## Contributing
+
+Feel free to contribute to this notebook. It's not perfect and additional
+functionality is planned.
diff --git a/oasst-shared/oasst_shared/exceptions/oasst_api_error.py b/oasst-shared/oasst_shared/exceptions/oasst_api_error.py
index 49eeb088..6cc25918 100644
--- a/oasst-shared/oasst_shared/exceptions/oasst_api_error.py
+++ b/oasst-shared/oasst_shared/exceptions/oasst_api_error.py
@@ -10,6 +10,7 @@ class OasstErrorCode(IntEnum):
0-1000: general errors
1000-2000: tasks endpoint
2000-3000: prompt_repository
+ 3000-4000: external resources
"""
# 0-1000: general errors
@@ -45,6 +46,9 @@ class OasstErrorCode(IntEnum):
TASK_ALREADY_DONE = 2105
TASK_NOT_COLLECTIVE = 2106
+ # 3000-4000: external resources
+ HUGGINGFACE_API_ERROR = 3001
+
class OasstError(Exception):
"""Base class for Open-Assistant exceptions."""
diff --git a/scripts/frontend-development/run-contract-test.sh b/scripts/frontend-development/run-contract-test.sh
new file mode 100755
index 00000000..6bedc903
--- /dev/null
+++ b/scripts/frontend-development/run-contract-test.sh
@@ -0,0 +1,11 @@
+#!/usr/bin/env bash
+parent_path=$( cd "$(dirname "${BASH_SOURCE[0]}")" ; pwd -P )
+
+# switch to website directory
+pushd "$parent_path/../../website"
+
+set -xe
+
+npm run cypress:run:contract
+
+popd
diff --git a/scripts/postprocessing/infogain_selector.py b/scripts/postprocessing/infogain_selector.py
index 4eedbc5c..fc644a83 100644
--- a/scripts/postprocessing/infogain_selector.py
+++ b/scripts/postprocessing/infogain_selector.py
@@ -1,9 +1,11 @@
import numpy as np
-from scipy import log2
-from scipy.integrate import nquad
from scipy.special import gammaln, psi
from scipy.stats import dirichlet
+'''
+Legacy numerical solution.
+Should not be used as it is probably broken
+
def make_range(*x):
"""
@@ -38,6 +40,23 @@ def naive_monte_carlo_integral(fun, dim, samples=10_000_000):
res = fun(pos)
return np.mean(res)
+def infogain(a_post, a_prior):
+ raise (
+ """For the love of good don't use this:
+ it's insanely poorly conditioned, the worst numerical code I have ever written
+ and it's slow as molasses. Use the analytic solution instead.
+
+ Maybe remove
+ """
+ )
+ args = len(a_prior)
+ p = dirichlet(a_post).pdf
+ q = dirichlet(a_prior).pdf
+ (info, _) = nquad(relative_entropy(p, q), [make_range for _ in range(args - 1)], opts={"epsabs": 1e-8})
+ # info = naive_monte_carlo_integral(relative_entropy(p,q), len(a_post))
+ return info
+'''
+
def analytic_solution(a_post, a_prior):
"""
@@ -57,26 +76,8 @@ def analytic_solution(a_post, a_prior):
return info
-def infogain(a_post, a_prior):
- raise (
- """For the love of good don't use this:
- it's insanely poorly conditioned, the worst numerical code I have ever written
- and it's slow as molasses. Use the analytic solution instead.
-
- Maybe remove
- """
- )
- args = len(a_prior)
- p = dirichlet(a_post).pdf
- q = dirichlet(a_prior).pdf
- (info, _) = nquad(relative_entropy(p, q), [make_range for _ in range(args - 1)], opts={"epsabs": 1e-8})
- # info = naive_monte_carlo_integral(relative_entropy(p,q), len(a_post))
- return info
-
-
def uniform_expected_infogain(a_prior):
mean_weight = dirichlet.mean(a_prior)
- print("weight", mean_weight)
results = []
for i, w in enumerate(mean_weight):
a_post = a_prior.copy()
diff --git a/scripts/postprocessing/scoring.py b/scripts/postprocessing/scoring.py
index efd236ce..5e76d19c 100644
--- a/scripts/postprocessing/scoring.py
+++ b/scripts/postprocessing/scoring.py
@@ -87,8 +87,9 @@ def score_update_prompts(consensus: npt.ArrayLike, voter_data: Voter) -> Voter:
"""
This function returns the gain of points for a given prompt's votes
- This function is only to be run when archiving a question
- i.e. the question has had sufficiently many votes, or we cann't get more than "K" bits of information
+ In contrast to the other score updating functions, we can run this online as new votes come in.
+ i.e. the question has had sufficiently many votes, or we cann't get more than "K" bits of information.
+
Parameters:
consensus (ArrayLike): all votes cast for this question
@@ -100,7 +101,8 @@ def score_update_prompts(consensus: npt.ArrayLike, voter_data: Voter) -> Voter:
# produces the ranking of votes, e.g. for [100,300,200] it returns [0, 2, 1],
# since 100 is the lowest, 300 the highest and 200 the middle value
consensus_ranking = np.arange(len(consensus)) - len(consensus) // 2 + 1
- delta_votes = np.sum(consensus_ranking * consensus)
+ # expected consenus ranking (i.e. normalize the votes and multiply-sum with weightings)
+ delta_votes = np.sum(consensus_ranking * consensus / sum(consensus))
new_points = delta_votes + voter_data.prompt_points
# we need to correct for 0 indexing, if you are closer to "right" than "wrong" of the conensus,
@@ -133,7 +135,7 @@ def score_update_ranking(user_ranking: npt.ArrayLike, consensus_ranking: npt.Arr
"research design and statistical analyses, second edition, 2003"
the authors note that at least from an significance test POV they will yield the same p-values
- Parameters:
+ Parameters:
user_ranking (ArrayLike): ranking produced by the user
consensus (ArrayLike): ranking produced after running the voting algorithm to merge into the consensus ranking
voter_data (Voter): a "Voter" object that represents the person that wrote the prompt
diff --git a/scripts/postprocessing/task_schedule.py b/scripts/postprocessing/task_schedule.py
new file mode 100644
index 00000000..deb302b2
--- /dev/null
+++ b/scripts/postprocessing/task_schedule.py
@@ -0,0 +1,75 @@
+from enum import Enum
+
+import numpy as np
+from scipy import optimize
+
+
+class Task(Enum):
+ RANKING = 0
+ ANSWER = 1
+ PROMPT = 2
+ VOTE = 3
+
+
+def task_selection(
+ num_ranking_tasks: int, current_prompts: int, target_num_prompts: int, p: float, answers_per_prompt: int
+) -> Task:
+ """
+ This computes which task to serve to the user.
+ In general, this method aims to get rankable tasks out of the active pool ASAP.
+ Before checking anything else, we first have a p% probability of running a ranking task.
+ After that, we can dynamically determine which task to serve by balancing the number of active tasks.
+
+ Parameters:
+ num_ranking_tasks (int): number of prompts that are ready to do ranking (i.e. have "answers_per_prompt" many answers)
+ current_prompts (int): how many prompts are currently in the active pool
+ target_num_prompts (int): how many prompts _should_ be in the active pool
+ p (float): probability to serve a ranking task, if one is available
+ answers_per_prompt (int): number of answers we want to have per prompt
+ Returns:
+ task (Task): the task Enum that corresponds to one of the four tasks
+ """
+ if num_ranking_tasks > 0 and np.random.rand() < p:
+ return Task.RANKING
+ rate = 50 / (current_prompts * 2)
+ prob_prompt_task = 0.5 + (target_num_prompts - current_prompts) * rate
+ # Yes, I'm too lazy to solve this analytically...
+ prob_unfinished_prompt = optimize.linprog(
+ np.array([1, 1]), A_eq=np.array([[1, 1], [1, -answers_per_prompt]]), b_eq=np.array([1, 0]), bounds=(0, None)
+ ).x[0]
+ if np.random.rand() < prob_prompt_task:
+ if np.random.rand() < prob_unfinished_prompt:
+ return Task.ANSWER
+ else:
+ return Task.PROMPT
+ else:
+ return Task.VOTE
+
+
+def next_answer_task(possible_prompts, answers_per_prompt):
+ """
+ If the `task_selection`method returns "answer", you can use this method to decide which
+ prompt should get an answer next.
+ The goal of this is to finish off the prompts that have almost enough answers collected already:
+ I.e. if we want 5 answers, this is going to give preferential sampling to those prompts that already
+ have 4/5 answers.
+ This helps to not have too much close-to-finished prompts in the active set.
+
+ Parameters:
+ possible_prompts (dict[prompt_id, num_answers]): a dictonary containing all open prompts and the number of answers these prompts currently have.
+ answers_per_prompt (int): number of answers we per prompt to target
+ Returns:
+ prompt_id (int): the prompt_id corresponding to the next prompt that should get a new answer
+ """
+ nums = list(set(possible_prompts.values()))
+ p = np.array([max(x / answers_per_prompt, 1 / answers_per_prompt) for x in nums])
+ idx = np.random.choice(nums, p=p / p.sum())
+ sample = np.random.choice([k for k, v in possible_prompts.items() if v == idx])
+ return sample
+
+
+if __name__ == "__main__":
+ x = task_selection(1, 500, 1000, 0.1, 5)
+ print(x)
+ y = next_answer_task({"this": 2, "is": 4, "a": 1, "test": 4}, 5)
+ print(y)
diff --git a/website/.env b/website/.env
index 9544836b..65d8b88e 100644
--- a/website/.env
+++ b/website/.env
@@ -1,3 +1,5 @@
+ADMIN_USERS = "credentials:admin,discord:root,email:admin@example.com"
+
# The database created by running the jobs in /scripts/frontend-development/docker-compose.yaml
DATABASE_URL=postgres://postgres:postgres@localhost:5433/oasst_web
diff --git a/website/README.md b/website/README.md
index 5198a820..11e3ccc4 100644
--- a/website/README.md
+++ b/website/README.md
@@ -53,7 +53,7 @@ If you're doing active development we suggest the following workflow:
1. Run `docker compose up frontend-dev --build --attach-dependencies`. You can
optionally include `-d` to detach and later track the logs if desired.
1. In another tab navigate to `${OPEN_ASSISTANT_ROOT/website`.
-1. Run `npm install`
+1. Run `npm ci`
1. Run `npx prisma db push` (This is also needed when you restart the docker
stack from scratch).
1. Run `npm run dev`. Now the website is up and running locally at
diff --git a/website/cypress.config.contract.js b/website/cypress.config.contract.js
new file mode 100644
index 00000000..f4461158
--- /dev/null
+++ b/website/cypress.config.contract.js
@@ -0,0 +1,9 @@
+import { defineConfig } from "cypress";
+
+export default defineConfig({
+ e2e: {
+ // No baseUrl here, because we don't need it for contract testing
+ baseUrl: null,
+ specPattern: "cypress/contract/*.cy.{ts,js}",
+ },
+});
diff --git a/website/cypress/contract/oasst_api_contract_tests.cy.ts b/website/cypress/contract/oasst_api_contract_tests.cy.ts
new file mode 100644
index 00000000..1ba408d6
--- /dev/null
+++ b/website/cypress/contract/oasst_api_contract_tests.cy.ts
@@ -0,0 +1,50 @@
+import { OasstApiClient } from "src/lib/oasst_api_client";
+
+describe("Contract test for Oasst API", function () {
+ // Assumes this is running the mock server.
+ const oasstApiClient = new OasstApiClient("http://localhost:8080", "test");
+
+ it("can fetch a task", async () => {
+ expect(
+ await oasstApiClient.fetchTask("random", {
+ sub: "test",
+ name: "test",
+ email: "test",
+ })
+ ).to.be.not.null;
+ });
+
+ it("can ack a task", async () => {
+ const task = await oasstApiClient.fetchTask("random", {
+ sub: "test",
+ name: "test",
+ email: "test",
+ });
+ expect(await oasstApiClient.ackTask(task.id, "321")).to.be.null;
+ });
+
+ it("can record a taskInteraction", async () => {
+ const task = await oasstApiClient.fetchTask("random", {
+ sub: "test",
+ name: "test",
+ email: "test",
+ });
+ expect(
+ await oasstApiClient.interactTask(
+ "text_reply_to_message",
+ task.id,
+ "1",
+ { text: "Test" },
+ {
+ sub: "test",
+ name: "test",
+ email: "test",
+ }
+ )
+ ).to.be.not.null;
+ });
+
+ // TODO(#354): Add test for 204
+ // TODO(#354): Add test for parsing >=300, throwing an OasstError
+ // TODO(#354): Add test for parsing >=300, throwing a generic error
+});
diff --git a/website/package.json b/website/package.json
index c66e10ca..7d3b680e 100644
--- a/website/package.json
+++ b/website/package.json
@@ -12,6 +12,7 @@
"build-storybook": "build-storybook",
"cypress": "cypress open",
"cypress:run": "cypress run",
+ "cypress:run:contract": "cypress run --config-file ./cypress.config.contract.js",
"cypress:image-baseline": "cypress-image-diff -u",
"fix:lint": "eslint --fix src/ --ext .js,.jsx,.ts,.tsx",
"fix:format": "prettier --write ./src",
diff --git a/website/prisma/schema.prisma b/website/prisma/schema.prisma
index 3e379d43..f9eab3b7 100644
--- a/website/prisma/schema.prisma
+++ b/website/prisma/schema.prisma
@@ -41,6 +41,7 @@ model User {
email String? @unique
emailVerified DateTime?
image String?
+ role String @default("general")
accounts Account[]
sessions Session[]
diff --git a/website/src/components/Dashboard/TaskOption.tsx b/website/src/components/Dashboard/TaskOption.tsx
index 8cf9977f..50f707a6 100644
--- a/website/src/components/Dashboard/TaskOption.tsx
+++ b/website/src/components/Dashboard/TaskOption.tsx
@@ -2,6 +2,12 @@ import { Box, Flex, GridItem, Heading, SimpleGrid, Text, useColorModeValue } fro
import Link from "next/link";
const crTasks = [
+ {
+ label: "Create Initial Prompts",
+ desc: "Write initial prompts to help Open Assistant to try replying to diverse messages.",
+ type: "create",
+ pathname: "/create/initial_prompt",
+ },
{
label: "Reply as User",
desc: "Chat with Open Assistant and help improve it’s responses as you interact with it.",
diff --git a/website/src/components/Survey/TaskControls.tsx b/website/src/components/Survey/TaskControls.tsx
index a93889ea..851e659c 100644
--- a/website/src/components/Survey/TaskControls.tsx
+++ b/website/src/components/Survey/TaskControls.tsx
@@ -4,7 +4,7 @@ import { SkipButton } from "src/components/Buttons/Skip";
import { SubmitButton } from "src/components/Buttons/Submit";
import { TaskInfo } from "src/components/TaskInfo/TaskInfo";
-interface TaskControlsProps {
+export interface TaskControlsProps {
// we need a task type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
tasks: any[];
@@ -30,11 +30,11 @@ export const TaskControls = (props: TaskControlsProps) => {
Skip
{endTask.task.type !== "task_done" ? (
- props.onSubmitResponse(props.tasks[0])}>
+ props.onSubmitResponse(props.tasks[0])}>
Submit
) : (
-
+
Next Task
)}
diff --git a/website/src/components/Survey/TaskControlsOverridable.tsx b/website/src/components/Survey/TaskControlsOverridable.tsx
new file mode 100644
index 00000000..b5333087
--- /dev/null
+++ b/website/src/components/Survey/TaskControlsOverridable.tsx
@@ -0,0 +1,60 @@
+import {
+ Button,
+ Flex,
+ Modal,
+ ModalBody,
+ ModalCloseButton,
+ ModalContent,
+ ModalFooter,
+ ModalHeader,
+ ModalOverlay,
+ useDisclosure,
+} from "@chakra-ui/react";
+import { TaskControls, TaskControlsProps } from "./TaskControls";
+
+interface TaskControlsOverridableProps extends TaskControlsProps {
+ isValid: boolean;
+ prepareForSubmit: () => void;
+}
+
+export const TaskControlsOverridable = (props: TaskControlsOverridableProps) => {
+ const { isValid, onSubmitResponse, ...rest } = props;
+ const { isOpen: isModalOpen, onOpen: onOpenModal, onClose: onModalClose } = useDisclosure();
+
+ const unchangedResponsePrompt = () => {
+ onOpenModal();
+
+ // Ideally this happens when the user clicks submit, but we can't
+ // reliably wait for it to be executed before submitting the response
+ // without significant refactoring.
+ // As a result, modal will only display once even if the user doesn't proceed
+ props.prepareForSubmit();
+ };
+
+ const onSubmitResponseOverride = () => {
+ onSubmitResponse(props.tasks[0]);
+ onModalClose();
+ };
+
+ return (
+ <>
+
+
+
+
+ Order Unchanged
+ You have not changed the order of the prompts. Are you sure you would like to submit?
+
+
+
+
+
+
+
+
+
+ >
+ );
+};
diff --git a/website/src/lib/oasst_api_client.ts b/website/src/lib/oasst_api_client.ts
new file mode 100644
index 00000000..7e22544a
--- /dev/null
+++ b/website/src/lib/oasst_api_client.ts
@@ -0,0 +1,98 @@
+import { JWT } from "next-auth/jwt";
+
+declare global {
+ // eslint-disable-next-line no-var
+ var oasstApiClient: OasstApiClient | undefined;
+}
+
+class OasstError {
+ message: string;
+ errorCode: number;
+ httpStatusCode: number;
+
+ constructor(message: string, errorCode: number, httpStatusCode: number) {
+ this.message = message;
+ this.errorCode = errorCode;
+ this.httpStatusCode = httpStatusCode;
+ }
+}
+
+export class OasstApiClient {
+ constructor(private readonly oasstApiUrl: string, private readonly oasstApiKey: string) {}
+
+ private async post(path: string, body: any): Promise {
+ const resp = await fetch(`${this.oasstApiUrl}${path}`, {
+ method: "POST",
+ headers: {
+ "X-API-Key": this.oasstApiKey,
+ "Content-Type": "application/json",
+ },
+ body: JSON.stringify(body),
+ });
+
+ if (resp.status == 204) {
+ return null;
+ }
+
+ if (resp.status >= 300) {
+ const errorText = await resp.text();
+ try {
+ const error = JSON.parse(errorText);
+ throw new OasstError(error.message, error.error_code, resp.status);
+ } catch (e) {
+ throw new OasstError(errorText, 0, resp.status);
+ }
+ }
+
+ return await resp.json();
+ }
+
+ // TODO return a strongly typed Task?
+ // This method is used to store a task in RegisteredTask.task.
+ // This is a raw Json type, so we can't use it to strongly type the task.
+ async fetchTask(taskType: string, userToken: JWT): Promise {
+ return this.post("/api/v1/tasks/", {
+ type: taskType,
+ user: {
+ id: userToken.sub,
+ display_name: userToken.name || userToken.email,
+ auth_method: "local",
+ },
+ });
+ }
+
+ async ackTask(taskId: string, messageId: string): Promise {
+ return this.post(`/api/v1/tasks/${taskId}/ack`, {
+ message_id: messageId,
+ });
+ }
+
+ // TODO return a strongly typed Task?
+ // This method is used to record interaction with task while fetching next task.
+ // This is a raw Json type, so we can't use it to strongly type the task.
+ async interactTask(
+ updateType: string,
+ messageId: string,
+ userMessageId: string,
+ content: object,
+ userToken: JWT
+ ): Promise {
+ return this.post("/api/v1/tasks/interaction", {
+ type: updateType,
+ user: {
+ id: userToken.sub,
+ display_name: userToken.name || userToken.email,
+ auth_method: "local",
+ },
+ message_id: messageId,
+ user_message_id: userMessageId,
+ ...content,
+ });
+ }
+}
+
+export const oasstApiClient =
+ globalThis.oasstApiClient || new OasstApiClient(process.env.FASTAPI_URL, process.env.FASTAPI_KEY);
+if (process.env.NODE_ENV !== "production") {
+ globalThis.oasstApiClient = oasstApiClient;
+}
diff --git a/website/src/pages/api/auth/[...nextauth].ts b/website/src/pages/api/auth/[...nextauth].ts
index 8614de97..363c1404 100644
--- a/website/src/pages/api/auth/[...nextauth].ts
+++ b/website/src/pages/api/auth/[...nextauth].ts
@@ -59,6 +59,17 @@ if (boolean(process.env.DEBUG_LOGIN) || process.env.NODE_ENV === "development")
);
}
+// Create a map of provider types to a set of admin user identifiers based on
+// the environment variables. We assume the list is separated by ',' and each
+// entry is separated by ':'.
+const adminUserMap = process.env.ADMIN_USERS.split(",").reduce((result, entry) => {
+ const [authType, id] = entry.split(":");
+ const s = result.get(authType) || new Set();
+ s.add(id);
+ result.set(authType, s);
+ return result;
+}, new Map());
+
export const authOptions: AuthOptions = {
// Ensure we can store user data in a database.
adapter: PrismaAdapter(prisma),
@@ -68,6 +79,56 @@ export const authOptions: AuthOptions = {
verifyRequest: "/auth/verify",
// error: "/auth/error", -Will be used later
},
+ callbacks: {
+ /**
+ * Ensure we propagate the user's role when creating the session from the
+ * token.
+ */
+ async session({ session, token }) {
+ session.user.role = token.role;
+ return session;
+ },
+ /**
+ * When creating a token, fetch the user's role and inject it in the token.
+ * This let's use forward the role to the session object.
+ */
+ async jwt({ token }) {
+ const { role } = await prisma.user.findUnique({
+ where: { id: token.sub },
+ select: { role: true },
+ });
+ token.role = role;
+ return token;
+ },
+ },
+ events: {
+ /**
+ * Update the user's role after they have successfully signed in
+ */
+ async signIn({ user, account }) {
+ // Get the admin list for the user's auth type.
+ const adminForAccountType = adminUserMap.get(account.provider);
+
+ // Return early if there's no admin list.
+ if (!adminForAccountType) {
+ return;
+ }
+
+ // TODO(#236): Reduce the number of times we update the role field.
+
+ // Update the database if the user is an admin.
+ if (adminForAccountType.has(account.providerAccountId)) {
+ await prisma.user.update({
+ data: {
+ role: "admin",
+ },
+ where: {
+ id: user.id,
+ },
+ });
+ }
+ },
+ },
session: {
strategy: "jwt",
},
diff --git a/website/src/pages/api/new_task/[task_type].ts b/website/src/pages/api/new_task/[task_type].ts
index 50f0b4e2..addcf3d8 100644
--- a/website/src/pages/api/new_task/[task_type].ts
+++ b/website/src/pages/api/new_task/[task_type].ts
@@ -1,4 +1,5 @@
import { getToken } from "next-auth/jwt";
+import { oasstApiClient } from "src/lib/oasst_api_client";
import prisma from "src/lib/prismadb";
/**
@@ -21,24 +22,7 @@ const handler = async (req, res) => {
}
// Fetch the new task.
- //
- // This needs to be refactored into an easier to use library.
- const taskRes = await fetch(`${process.env.FASTAPI_URL}/api/v1/tasks/`, {
- method: "POST",
- headers: {
- "X-API-Key": process.env.FASTAPI_KEY,
- "Content-Type": "application/json",
- },
- body: JSON.stringify({
- type: task_type,
- user: {
- id: token.sub,
- display_name: token.name || token.email,
- auth_method: "local",
- },
- }),
- });
- const task = await taskRes.json();
+ const task = await oasstApiClient.fetchTask(task_type, token);
// Store the task and link it to the user..
const registeredTask = await prisma.registeredTask.create({
@@ -53,16 +37,7 @@ const handler = async (req, res) => {
});
// Update the backend with our Task ID
- await fetch(`${process.env.FASTAPI_URL}/api/v1/tasks/${task.id}/ack`, {
- method: "POST",
- headers: {
- "X-API-Key": process.env.FASTAPI_KEY,
- "Content-Type": "application/json",
- },
- body: JSON.stringify({
- message_id: registeredTask.id,
- }),
- });
+ await oasstApiClient.ackTask(task.id, registeredTask.id);
// Send the results to the client.
res.status(200).json(registeredTask);
diff --git a/website/src/pages/api/update_task.ts b/website/src/pages/api/update_task.ts
index 9582040b..4eea8c1e 100644
--- a/website/src/pages/api/update_task.ts
+++ b/website/src/pages/api/update_task.ts
@@ -1,4 +1,5 @@
import { getToken } from "next-auth/jwt";
+import { oasstApiClient } from "src/lib/oasst_api_client";
import prisma from "src/lib/prismadb";
/**
@@ -34,27 +35,7 @@ const handler = async (req, res) => {
},
});
- // Send the interaction to the Task Backend. This automatically fetches the
- // next task in the sequence (or the done task).
- const interactionRes = await fetch(`${process.env.FASTAPI_URL}/api/v1/tasks/interaction`, {
- method: "POST",
- headers: {
- "X-API-Key": process.env.FASTAPI_KEY,
- "Content-Type": "application/json",
- },
- body: JSON.stringify({
- type: update_type,
- user: {
- id: token.sub,
- display_name: token.name || token.email,
- auth_method: "local",
- },
- message_id: id,
- user_message_id: interaction.id,
- ...content,
- }),
- });
- const newTask = await interactionRes.json();
+ const newTask = await oasstApiClient.interactTask(update_type, id, interaction.id, content, token);
// Stores the new task with our database.
const newRegisteredTask = await prisma.registeredTask.create({
diff --git a/website/src/pages/evaluate/rank_assistant_replies.tsx b/website/src/pages/evaluate/rank_assistant_replies.tsx
index 0ed69b09..d20f6364 100644
--- a/website/src/pages/evaluate/rank_assistant_replies.tsx
+++ b/website/src/pages/evaluate/rank_assistant_replies.tsx
@@ -6,7 +6,7 @@ import { LoadingScreen } from "src/components/Loading/LoadingScreen";
import { Message } from "src/components/Messages";
import { Sortable } from "src/components/Sortable/Sortable";
import { SurveyCard } from "src/components/Survey/SurveyCard";
-import { TaskControls } from "src/components/Survey/TaskControls";
+import { TaskControlsOverridable } from "src/components/Survey/TaskControlsOverridable";
import fetcher from "src/lib/fetcher";
import poster from "src/lib/poster";
import useSWRImmutable from "swr/immutable";
@@ -90,7 +90,13 @@ const RankAssistantReplies = () => {
-
+ setRanking(tasks[0].task.replies.map((_, idx) => idx))}
+ onSubmitResponse={submitResponse}
+ onSkip={fetchNextTask}
+ />
>
);
diff --git a/website/src/pages/evaluate/rank_initial_prompts.tsx b/website/src/pages/evaluate/rank_initial_prompts.tsx
index a9d590ac..a0a48b27 100644
--- a/website/src/pages/evaluate/rank_initial_prompts.tsx
+++ b/website/src/pages/evaluate/rank_initial_prompts.tsx
@@ -4,7 +4,7 @@ import { useEffect, useState } from "react";
import { LoadingScreen } from "src/components/Loading/LoadingScreen";
import { Sortable } from "src/components/Sortable/Sortable";
import { SurveyCard } from "src/components/Survey/SurveyCard";
-import { TaskControls } from "src/components/Survey/TaskControls";
+import { TaskControlsOverridable } from "src/components/Survey/TaskControlsOverridable";
import fetcher from "src/lib/fetcher";
import poster from "src/lib/poster";
import useSWRImmutable from "swr/immutable";
@@ -85,7 +85,13 @@ const RankInitialPrompts = () => {
-
+ setRanking(tasks[0].task.prompts.map((_, idx) => idx))}
+ onSubmitResponse={submitResponse}
+ onSkip={fetchNextTask}
+ />
>
);
diff --git a/website/src/pages/evaluate/rank_user_replies.tsx b/website/src/pages/evaluate/rank_user_replies.tsx
index 9a0577cb..09e24fa2 100644
--- a/website/src/pages/evaluate/rank_user_replies.tsx
+++ b/website/src/pages/evaluate/rank_user_replies.tsx
@@ -6,7 +6,7 @@ import { LoadingScreen } from "src/components/Loading/LoadingScreen";
import { Message } from "src/components/Messages";
import { Sortable } from "src/components/Sortable/Sortable";
import { SurveyCard } from "src/components/Survey/SurveyCard";
-import { TaskControls } from "src/components/Survey/TaskControls";
+import { TaskControlsOverridable } from "src/components/Survey/TaskControlsOverridable";
import fetcher from "src/lib/fetcher";
import poster from "src/lib/poster";
import useSWRImmutable from "swr/immutable";
@@ -89,7 +89,13 @@ const RankUserReplies = () => {
-
+ setRanking(tasks[0].task.replies.map((_, idx) => idx))}
+ onSubmitResponse={submitResponse}
+ onSkip={fetchNextTask}
+ />
>
);
diff --git a/website/src/pages/messages/index.tsx b/website/src/pages/messages/index.tsx
index 9ecdeb35..39430caf 100644
--- a/website/src/pages/messages/index.tsx
+++ b/website/src/pages/messages/index.tsx
@@ -41,9 +41,11 @@ const MessagesDashboard = () => {
-
+
- Most recent messages
+
+ Most recent messages
+ {
- Your most recent messages
+
+ Your most recent messages
+