Skip to content

Commit

Permalink
update chat docs
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-gee committed Nov 1, 2023
1 parent d79a352 commit 69eee86
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 52 deletions.
2 changes: 1 addition & 1 deletion src/webtranspose/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
__email__ = "[email protected]"
__version__ = "0.1.0"

from .chat import *
from .crawl import *
from .openai import *
from .scrape import *
from .chat import *
109 changes: 58 additions & 51 deletions src/webtranspose/chat.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,8 @@
import asyncio
import json
import logging
import os
import shutil
import tempfile
import urllib.parse
import uuid
import zipfile
from datetime import datetime
from fnmatch import fnmatch
from typing import Dict, List, Optional
from urllib.parse import urljoin, urlparse, urlunparse
from time import sleep
from typing import List

import httpx
from bs4 import BeautifulSoup

from .webt_api import run_webt_api

Expand All @@ -30,13 +18,24 @@ def __init__(
chatbot_id: str = None,
_created: bool = False,
) -> None:
"""
Initialize a Chatbot instance.
:param url_list: A list of URLs to crawl.
:param name: The name of the chatbot.
:param max_pages: The maximum number of pages to crawl.
:param api_key: The API key for accessing the Web Transpose API.
:param verbose: Whether to enable verbose logging.
:param chatbot_id: The ID of an existing chatbot.
:param _created: Whether the chatbot has already been created.
"""
self.api_key = api_key
if self.api_key is None:
self.api_key = os.environ.get("WEBTRANSPOSE_API_KEY")

if self.api_key is None:
raise ValueError(
"No Web Transpose API provided. \n\nTo Crawl on the Web Transpose API, set the WEBTRANSPOSE_API_KEY from https://webtranspose.com. Run cheaper with logging and advanced analytics."
"No Web Transpose API provided. \n\nTo use Chatbots, set the WEBTRANSPOSE_API_KEY from https://webtranspose.com."
)

self.url_list = url_list
Expand All @@ -50,10 +49,13 @@ def __init__(
self.create()

def create(self):
"""
Create a chatbot.
"""
if not self.chatbot_id:
self._create_chat()
status = self.status()
while status['status'] != 'complete':
while status["status"] != "complete":
if self.verbose:
logging.info("Waiting for chat to be created...")
sleep(5)
Expand All @@ -63,12 +65,18 @@ def create(self):
logging.info("Chat already created.")

def queue_create(self):
"""
Queue the creation of a chatbot.
"""
if not self.chatbot_id:
self._create_chat()
else:
logging.info("Chat already created.")

def _create_chat(self):
"""
Create a chat.
"""
if self.verbose:
logging.info("Creating chat...")

Expand All @@ -78,14 +86,17 @@ def _create_chat(self):
"max_pages": self.max_pages,
"url_list": self.url_list,
}
out_json = run_webt_api(
create_json,
"v1/chat/create",
self.api_key
)
self.chatbot_id = out_json['chatbot_id']

def query_database(self, query, num_records=3):
out_json = run_webt_api(create_json, "v1/chat/create", self.api_key)
self.chatbot_id = out_json["chatbot_id"]

def query_database(self, query: str, num_records: int = 3) -> list:
"""
Query the database of the chatbot.
:param query: The query string.
:param num_records: The number of records to return.
:return: The query results.
"""
if self.verbose:
logging.info("Querying database...")

Expand All @@ -97,14 +108,15 @@ def query_database(self, query, num_records=3):
"query": query,
"num_records": num_records,
}
out = run_webt_api(
query_json,
"v1/chat/database/query",
self.api_key
)
return out['results']
out = run_webt_api(query_json, "v1/chat/database/query", self.api_key)
return out["results"]

def status(self):
"""
Get the status of the chatbot.
:return: The chatbot status.
"""
if self.verbose:
logging.info("Getting chat...")

Expand All @@ -114,14 +126,15 @@ def status(self):
get_json = {
"chatbot_id": self.chatbot_id,
}
out = run_webt_api(
get_json,
"v1/chat/get",
self.api_key
)
return out['chatbot']

def add_urls(self, url_list):
out = run_webt_api(get_json, "v1/chat/get", self.api_key)
return out["chatbot"]

def add_urls(self, url_list: list):
"""
Add URLs to the chatbot.
:param url_list: A list of URLs to add.
"""
if self.verbose:
logging.info("Querying database...")

Expand All @@ -133,13 +146,14 @@ def add_urls(self, url_list):
"max_pages": self.max_pages,
"url_list": url_list,
}
run_webt_api(
query_json,
"v1/chat/urls/add",
self.api_key
)
run_webt_api(query_json, "v1/chat/urls/add", self.api_key)

def delete_crawls(self, crawl_id_list: list):
"""
Delete crawls from the chatbot.
def delete_crawls(self, crawl_id_list):
:param crawl_id_list: A list of crawl IDs to delete.
"""
if self.verbose:
logging.info("Querying database...")

Expand All @@ -150,11 +164,4 @@ def delete_crawls(self, crawl_id_list):
"chatbot_id": self.chatbot_id,
"crawl_id_list": crawl_id_list,
}
run_webt_api(
query_json,
"v1/chat/crawls/delete",
self.api_key
)



run_webt_api(query_json, "v1/chat/crawls/delete", self.api_key)

0 comments on commit 69eee86

Please sign in to comment.