Skip to content

Commit

Permalink
add: /api/ask
Browse files Browse the repository at this point in the history
  • Loading branch information
glorenzo972 committed Jul 1, 2024
1 parent e1d9d7b commit 9c505d5
Show file tree
Hide file tree
Showing 14 changed files with 1,027 additions and 99 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
*Andrea Sponziello*
### **Copyrigth**: *Tiledesk SRL*

## [2024-07-01]
### 0.2.4
- fix: scrape_type=0
- added: /api/ask to ask to llm


## [2024-06-21]
### 0.2.3
- fix: delete chunks from namespace by metadata id
Expand Down
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,26 @@ pc.create_index(const.PINECONE_INDEX,
)
)
```

## Models
### OpenAI - engine: openai
- gpt-3.5-turbo
- gpt-4
- gpt-4-turbo
- got-4o

### Cohere - engine: cohere
- command-r
- command-r-plus

### Google - engine: google
- gemini-pro

### Anthropic - engine: anthropic
- claude-3-5-sonnet-20240620

### Groq - engine: groq
- Llama3-70b-8192
- Llama3-8b-8192
- Mixtral-8x7b-32768
- Gemma-7b-It
33 changes: 20 additions & 13 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "tilellm"
version = "0.2.3"
version = "0.2.4"
description = "tiledesk for RAG"
authors = ["Gianluca Lorenzo <[email protected]>"]
repository = "https://github.com/Tiledesk/tiledesk-llm"
Expand All @@ -13,26 +13,33 @@ build-backend = "poetry.core.masonry.api"

[tool.poetry.dependencies]
python = "^3.10"
fastapi = "^0.110"
jsonschema= "^4.20.0"
redis= "^5.0.0"
aioredis= "^2.0.0"
fastapi = "^0.110.0"
jsonschema= "^4.22.0"
redis= "^5.0.7"
aioredis= "^2.0.1"
#redismutex = "^1.0.0"
langchain = "^0.1.16"
jq = "^1.6.0"
openai = "^1.12.0"
langchain_openai = "0.0.x"
pinecone-client = "^3.1.0"
langchain = "^0.2.6"
jq = "^1.7.0"
openai = "^1.35.7"
langchain_openai = "0.1.x"
langchain-voyageai = "0.1.1"
langchain-anthropic = "0.1.16"
langchain-cohere="0.1.8"
langchain-google-genai= "1.0.7"
langchain-groq ="0.1.6"
langchain-aws="0.1.8"
pinecone-client = "^4.1.1"
python-dotenv = "^1.0.1"
langchain_community = "0.0.x"
tiktoken = "0.6.x"
langchain_community = "0.2.x"
tiktoken = "0.7.x"
beautifulsoup4 ="^4.12.3"
#uvicorn = "^0.28"
unstructured= "^0.12.6"
unstructured= "0.14.x"
#playwright = "^1.43.0"
pypdf="^4.2.0"
docx2txt="^0.8"
wikipedia="^1.4.0"
psutil="^6.0.0"

[tool.poetry.dependencies.uvicorn]
version = "^0.28"
Expand Down
94 changes: 60 additions & 34 deletions tilellm/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,20 @@
PineconeNamespaceToDelete,
ScrapeStatusReq,
ScrapeStatusResponse,
PineconeIndexingResult)
PineconeIndexingResult, RetrievalResult, PineconeNamespaceResult,
PineconeDescNamespaceResult, PineconeItems, QuestionToLLM, SimpleAnswer)

from tilellm.store.redis_repository import redis_xgroup_create
from tilellm.controller.openai_controller import (ask_with_memory,
ask_with_sequence,
add_pc_item,
delete_namespace,
delete_id_from_namespace,
get_ids_namespace,
get_listitems_namespace,
get_desc_namespace,
get_list_namespace,
get_sources_namespace)
from tilellm.controller.controller import (ask_with_memory,
ask_with_sequence,
add_pc_item,
delete_namespace,
delete_id_from_namespace,
get_ids_namespace,
get_listitems_namespace,
get_desc_namespace,
get_list_namespace,
get_sources_namespace, ask_to_llm)

import logging

Expand Down Expand Up @@ -215,7 +216,7 @@ async def enqueue_scrape_item_main(item: ItemSingle, redis_client: aioredis.clie
enqueue item to redis. Consumer read message and add it to namespace
:param item:
:param redis_client:
:return:
:return: PineconeIndexingResult
"""
from tilellm.shared import const
logger.debug(item)
Expand All @@ -231,13 +232,14 @@ async def enqueue_scrape_item_main(item: ItemSingle, redis_client: aioredis.clie

return {"message": f"Item {item.id} created successfully, more {res}"}

@app.post("/api/scrape/single")

@app.post("/api/scrape/single", response_model=PineconeIndexingResult)
async def create_scrape_item_single(item: ItemSingle, redis_client: aioredis.client.Redis = Depends(get_redis_client)):
"""
Add item to namespace
:param item:
:param redis_client:
:return:
:return: PineconeIndexingResult
"""
webhook = ""
token = ""
Expand All @@ -248,8 +250,8 @@ async def create_scrape_item_single(item: ItemSingle, redis_client: aioredis.cli
status_code=2
)
add_to_queue = await redis_client.set(f"{item.namespace}/{item.id}",
scrape_status_response.model_dump_json(),
ex=expiration_in_seconds)
scrape_status_response.model_dump_json(),
ex=expiration_in_seconds)

logger.debug(f"Start {add_to_queue}")

Expand Down Expand Up @@ -290,8 +292,8 @@ async def create_scrape_item_single(item: ItemSingle, redis_client: aioredis.cli
status_code=3
)
add_to_queue = await redis_client.set(f"{item.namespace}/{item.id}",
scrape_status_response.model_dump_json(),
ex=expiration_in_seconds)
scrape_status_response.model_dump_json(),
ex=expiration_in_seconds)

# logger.debug(f"End {add_to_queue}")
# if webhook:
Expand All @@ -313,8 +315,8 @@ async def create_scrape_item_single(item: ItemSingle, redis_client: aioredis.cli
status_code=4
)
add_to_queue = await redis_client.set(f"{item.namespace}/{item.id}",
scrape_status_response.model_dump_json(),
ex=expiration_in_seconds)
scrape_status_response.model_dump_json(),
ex=expiration_in_seconds)

logger.error(f"Error {add_to_queue}")
import traceback
Expand All @@ -330,27 +332,48 @@ async def create_scrape_item_single(item: ItemSingle, redis_client: aioredis.cli
logger.error(e)
raise HTTPException(status_code=400, detail=repr(e))

@app.post("/api/qa")

@app.post("/api/qa", response_model=RetrievalResult)
async def post_ask_with_memory_main(question_answer: QuestionAnswer):
"""
Query and Aswer with chat history
:param question_answer:
:return: RetrievalResult
"""
logger.debug(question_answer)

result = await ask_with_memory(question_answer)

logger.debug(result)
return JSONResponse(content=result)
return JSONResponse(content=result.model_dump())


@app.post("/api/qachain")
@app.post("/api/ask", response_model=SimpleAnswer)
async def post_ask_to_llm_main(question: QuestionToLLM):
"""
Query and Answer with a LLM
:param question:
:return: RetrievalResult
"""
logger.info(question)

result = await ask_to_llm(question=question)

logger.debug(result)
return JSONResponse(content=result.model_dump())


@app.post("/api/qachain", response_model=RetrievalResult)
async def post_ask_with_memory_chain_main(question_answer: QuestionAnswer):
print(question_answer)
logger.debug(question_answer)
result = ask_with_sequence(question_answer)
result = await ask_with_sequence(question_answer)
logger.debug(result)
return JSONResponse(content=result)
return JSONResponse(content=result.model_dump())
# return result


@app.get("/api/list/namespace")
@app.get("/api/list/namespace", response_model=PineconeNamespaceResult)
async def list_namespace_main():
"""
Get all namespaces with id and vector count
Expand All @@ -364,7 +387,8 @@ async def list_namespace_main():
logger.error(ex)
raise HTTPException(status_code=400, detail=repr(ex))

@app.get("/api/desc/namespace/{namespace}")

@app.get("/api/desc/namespace/{namespace}", response_model=PineconeDescNamespaceResult)
async def list_namespace_items_main(namespace: str):
"""
Get description for given namespace
Expand All @@ -380,7 +404,8 @@ async def list_namespace_items_main(namespace: str):
logger.error(ex)
raise HTTPException(status_code=400, detail=repr(ex))

@app.get("/api/listitems/namespace/{namespace}")

@app.get("/api/listitems/namespace/{namespace}", response_model=PineconeItems)
async def list_namespace_items_main(namespace: str):
"""
Get all item with given namespace
Expand All @@ -397,7 +422,8 @@ async def list_namespace_items_main(namespace: str):
raise HTTPException(status_code=400, detail=repr(ex))


@app.post("/api/scrape/status")
@app.post("/api/scrape/status", response_model=
ScrapeStatusResponse)
async def scrape_status_main(scrape_status_req: ScrapeStatusReq,
redis_client: aioredis.client.Redis = Depends(get_redis_client)):
"""
Expand Down Expand Up @@ -502,7 +528,7 @@ async def delete_item_id_namespace_post(item_to_delete: PineconeItemToDelete):
# raise HTTPException(status_code=400, detail=repr(ex))


@app.get("/api/id/{metadata_id}/namespace/{namespace}")
@app.get("/api/id/{metadata_id}/namespace/{namespace}", response_model=PineconeItems)
async def get_items_id_namespace_main(metadata_id: str, namespace: str):
"""
Get all items from namespace given id of document
Expand All @@ -512,16 +538,16 @@ async def get_items_id_namespace_main(metadata_id: str, namespace: str):
"""
try:
logger.info(f"retrieve id {metadata_id} dal namespace {namespace}")
result = await get_ids_namespace(metadata_id,namespace)
result = await get_ids_namespace(metadata_id, namespace)

return JSONResponse(content=result.model_dump())
except Exception as ex:
logger.error(ex)
raise HTTPException(status_code=400, detail=repr(ex))


@app.get("/api/items")#?source={source}&namespace={namespace}
async def get_items_source_namespace_main(source: str, namespace: str ):
@app.get("/api/items", response_model=PineconeItems)#?source={source}&namespace={namespace}
async def get_items_source_namespace_main(source: str, namespace: str):
"""
Get all item given the source and namespace
:param source: source of document
Expand All @@ -533,7 +559,7 @@ async def get_items_source_namespace_main(source: str, namespace: str ):

from urllib.parse import unquote
source = unquote(source)
result = await get_sources_namespace(source,namespace)
result = await get_sources_namespace(source, namespace)

return JSONResponse(content=result.model_dump())
except Exception as ex:
Expand Down
Loading

0 comments on commit 9c505d5

Please sign in to comment.