Skip to content

Commit

Permalink
Update readmes; add .env files; do formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
ruthenian8 committed Nov 23, 2023
1 parent 1607b77 commit ac1ef93
Show file tree
Hide file tree
Showing 15 changed files with 34 additions and 32 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,3 @@ dbs
benchmarks
benchmark_results_files.json
uploaded_benchmarks
**/.env
File renamed without changes.
6 changes: 3 additions & 3 deletions examples/customer_service_bot/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ docker compose up --build --abort-on-container-exit --exit-code-from intent_clie
## Run the bot

### Run with Docker & Docker-Compose environment
To interact with external APIs, the bot requires API tokens that can be set through [.env](.env.example). You should start by creating your own `.env` file:
To interact with external APIs, the bot requires API tokens that can be set through the [.env](.env) file. Update it replacing templates with actual token values.
```
echo TG_BOT_TOKEN=*** >> .env
echo OPENAI_API_TOKEN=*** >> .env
TG_BOT_TOKEN=***
OPENAI_API_TOKEN=***
```

*The commands below need to be run from the /examples/customer_service_bot directory*
Expand Down
2 changes: 1 addition & 1 deletion examples/customer_service_bot/bot/dialog_graph/script.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
},
"ask_item": {
RESPONSE: Message(
text="Which books would you like to order? Please, separate the titles by commas (type 'abort' to cancel)."
text="Which books would you like to order? Separate the titles by commas (type 'abort' to cancel)"
),
PRE_TRANSITIONS_PROCESSING: {"1": loc_prc.extract_item()},
TRANSITIONS: {("form_flow", "ask_delivery"): loc_cnd.slots_filled(["items"]), lbl.repeat(0.8): cnd.true()},
Expand Down
4 changes: 3 additions & 1 deletion examples/customer_service_bot/bot/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
(
TelegramMessage(text="card"),
Message(
text="We registered your transaction. Requested titles are: Pale Fire, Lolita. Delivery method: deliver. Payment method: card. Type `abort` to cancel, type `ok` to continue."
text="We registered your transaction. Requested titles are: Pale Fire, Lolita. "
"Delivery method: deliver. Payment method: card. "
"Type `abort` to cancel, type `ok` to continue."
),
),
(TelegramMessage(text="ok"), script["chitchat_flow"]["init_chitchat"][RESPONSE]),
Expand Down
8 changes: 4 additions & 4 deletions examples/customer_service_bot/intent_catcher/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@
else:
no_cuda = True
model = AutoModelForSequenceClassification.from_config(DistilBertConfig(num_labels=23))
state = torch.load(MODEL_PATH, map_location = "cpu" if no_cuda else "gpu")
state = torch.load(MODEL_PATH, map_location="cpu" if no_cuda else "gpu")
model.load_state_dict(state["model_state_dict"])
tokenizer = AutoTokenizer.from_pretrained("distilbert-base-uncased")
pipe = pipeline('text-classification', model=model, tokenizer=tokenizer)
pipe = pipeline("text-classification", model=model, tokenizer=tokenizer)
logger.info("predictor is ready")
except Exception as e:
sentry_sdk.capture_exception(e)
Expand Down Expand Up @@ -71,9 +71,9 @@ def respond():

try:
results = pipe(contexts)
indices = [int(''.join(filter(lambda x: x.isdigit(), result['label']))) for result in results]
indices = [int("".join(filter(lambda x: x.isdigit(), result["label"]))) for result in results]
responses = [list(label2id.keys())[idx] for idx in indices]
confidences = [result['score'] for result in results]
confidences = [result["score"] for result in results]
except Exception as exc:
logger.exception(exc)
sentry_sdk.capture_exception(exc)
Expand Down
1 change: 1 addition & 0 deletions examples/frequently_asked_question_bot/telegram/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TG_BOT_TOKEN=tg_bot_token

This file was deleted.

6 changes: 4 additions & 2 deletions examples/frequently_asked_question_bot/telegram/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ An example of bot usage:
![image](https://user-images.githubusercontent.com/61429541/219064505-20e67950-cb88-4cff-afa5-7ce608e1282c.png)

### Run with Docker & Docker-Compose environment
In order for the bot to work, set the bot token via [.env](.env.example). First step is creating your `.env` file:

In order for the bot to work, update the [.env](.env) file replacing the template with the actual value of your Telegram token.

```
echo TG_BOT_TOKEN=******* >> .env
TG_BOT_TOKEN=***
```

Build the bot:
Expand Down
3 changes: 3 additions & 0 deletions examples/frequently_asked_question_bot/web/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
POSTGRES_USERNAME=postgres
POSTGRES_PASSWORD=pass
POSTGRES_DB=test
7 changes: 6 additions & 1 deletion examples/frequently_asked_question_bot/web/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ A showcase of the website:
![faq_web](https://user-images.githubusercontent.com/61429541/233875303-b9bc81c9-522b-4596-8599-6efcfa708d1e.gif)

### Run with Docker & Docker-Compose environment

The Postgresql image needs to be configured with variables that can be set through the [.env](.env) file. Update the file replacing templates with desired values.
```
POSTGRES_USERNAME=***
POSTGRES_PASSWORD=***
POSTGRES_DB=***
```
Build the bot:
```commandline
docker-compose build
Expand Down
9 changes: 3 additions & 6 deletions examples/frequently_asked_question_bot/web/web/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

@app.get("/")
async def index():
return FileResponse('static/index.html', media_type='text/html')
return FileResponse("static/index.html", media_type="text/html")


@app.websocket("/ws/{client_id}")
Expand All @@ -20,11 +20,7 @@ async def websocket_endpoint(websocket: WebSocket, client_id: str):

# store user info in the dialogue context
await pipeline.context_storage.set_item_async(
client_id,
Context(
id=client_id,
misc={"ip": websocket.client.host, "headers": websocket.headers.raw}
)
client_id, Context(id=client_id, misc={"ip": websocket.client.host, "headers": websocket.headers.raw})
)

async def respond(request: Message):
Expand All @@ -42,6 +38,7 @@ async def respond(request: Message):
except WebSocketDisconnect: # ignore disconnects
pass


if __name__ == "__main__":
uvicorn.run(
app,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,15 @@ def get_bot_answer(question: str) -> Message:


FALLBACK_ANSWER = Message(
text='I don\'t have an answer to that question. '
'You can find FAQ <a href="https://wiki.archlinux.org/title/Frequently_asked_questions">here</a>.',
text="I don't have an answer to that question. "
'You can find FAQ <a href="https://wiki.archlinux.org/title/Frequently_asked_questions">here</a>.',
)
"""Fallback answer that the bot returns if user's query is not similar to any of the questions."""


FIRST_MESSAGE = Message(
text="Welcome! Ask me questions about Arch Linux."
)
FIRST_MESSAGE = Message(text="Welcome! Ask me questions about Arch Linux.")

FALLBACK_NODE_MESSAGE = Message(
text="Something went wrong.\n"
"You may continue asking me questions about Arch Linux."
)
FALLBACK_NODE_MESSAGE = Message(text="Something went wrong.\n" "You may continue asking me questions about Arch Linux.")


def answer_similar_question(ctx: Context, _: Pipeline):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,11 @@
},
"service_flow": {
"start_node": {}, # this is the start node, it simply redirects to welcome node

"fallback_node": { # this node will only be used if something goes wrong (e.g. an exception is raised)
RESPONSE: FALLBACK_NODE_MESSAGE,
},
},
},
"start_label": ("service_flow", "start_node"),
"fallback_label": ("service_flow", "fallback_node")
"fallback_label": ("service_flow", "fallback_node"),
}
2 changes: 1 addition & 1 deletion examples/frequently_asked_question_bot/web/web/bot/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@
async def test_happy_path(happy_path):
check_happy_path(
pipeline=Pipeline.from_script(**script.pipeline_kwargs, pre_services=pre_services.services),
happy_path=happy_path
happy_path=happy_path,
)

0 comments on commit ac1ef93

Please sign in to comment.