Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DX Copilot #12

Merged
merged 2 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,17 @@ $ curl \
https://ai-ml.fly.dev/neighbours
```

## Ask

Ask AI engine to generate an answer to the question.

```bash
$ curl \
-X POST \
--data '{"q":"What is Shopware?"}' \
https://ai-ml.fly.dev/question
```

# Notes

Notes:
Expand Down
24 changes: 22 additions & 2 deletions cli/main.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
from typing import List, Union
from rich import print
from rich.console import Console
from rich.markdown import Markdown
import typer
import os

# Disable tensorflow warn prompts
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"

from web.ingest import ingest_url
from web.ingest import ingest_url, ingest
from web.query import query, map_results
from .answering import text_gen

app = typer.Typer()


@app.command("ingest-url")
def cmd_ingest_url(url: str, collection: Union[str, None]):
def cmd_ingest_url(url: str, collection: Union[str, None] = typer.Option(None)):
ingest_url(url, collection)


@app.command("ingest")
def cmd_ingest_(collection: Union[str, None] = typer.Option(None)):
ingest(collection)


@app.command("query")
def cmd_query(search: str, collections: List[str] = typer.Option([])):
console = Console()
Expand All @@ -33,5 +40,18 @@ def cmd_query(search: str, collections: List[str] = typer.Option([])):
print(results)


@app.command("question")
def cmd_generate_text(plain: bool = typer.Option(False)):
person_name = typer.prompt("What is your question?")
answer = text_gen(person_name)
# text = ''.join([par["text"] for par in paragraphs])
if plain:
print(text)
else:
print("")
# print(Markdown(text))
print(answer)


def main():
app()
32 changes: 32 additions & 0 deletions web/answering.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from langchain.llms import OpenAI
from langchain.prompts import PromptTemplate
from langchain.chains import VectorDBQAWithSourcesChain

from web.config import get_embedding_fn, db_dir
from web.vector_store import FaissMap

from langchain.chains import LLMChain


def generate_answer(question: str, collection=None):
search_index = FaissMap.load_local(db_dir(collection), get_embedding_fn())

prompt_template = """Use the context below to provide a detailed answer for the question below:
Context: {context}
Answer:"""

PROMPT = PromptTemplate(
template=prompt_template, input_variables=["context"]
)

llm = OpenAI(temperature=0.1, max_tokens=512)

# chain = LLMChain(llm=llm, prompt=PROMPT)
chain = VectorDBQAWithSourcesChain.from_chain_type(
llm, chain_type="map_reduce", vectorstore=search_index
)

# docs = search_index.similarity_search(question, k=4)
# inputs = [{"context": doc.page_content, "question": question} for doc in docs]
# return chain.apply(inputs)
return chain({"question": question}, return_only_outputs=True)
11 changes: 11 additions & 0 deletions web/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import os
import glob
import shutil
import typer

from .upload import upload
from .query import query, query_by_id, query_n_with_fallback, map_results, unique_results
Expand All @@ -24,7 +25,9 @@
PostQueryParams,
PostNeighboursParams,
PostURLIngestParams,
QuestionParams,
)
from .answering import generate_answer
from .results import Results, Result, Success, SuccessWithMetadatas, Hello, Status

import logging
Expand Down Expand Up @@ -69,6 +72,7 @@
{"name": "cache", "description": "Delete old cache from the filesystem"},
{"name": "storage", "description": "Get storage usage"},
{"name": "download", "description": "Download <db> or <docs> dir for <collection>"},
{"name": "question", "description": "Q&A endpoint"},
{"name": "healthcheck", "description": "Healthcheck endpoint"},
]

Expand Down Expand Up @@ -193,6 +197,13 @@ def get_storage(
return get_download(type, collection)


@app.post("/question", tags=["question"])
def post_question(
data: QuestionParams,
):
return generate_answer(data.q)


# https://ahmadrosid.com/blog/deploy-fastapi-flyio
# https://fly.io/docs/languages-and-frameworks/python/
# https://fly.io/docs/languages-and-frameworks/dockerfile/
Expand Down
4 changes: 4 additions & 0 deletions web/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,9 @@ class Config:
}


class QuestionParams(BaseModel):
q: str


class PostURLIngestParams(CollectionParam, URLParam):
pass