Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Setting up connections to OpenAI and Pinecone

Documentation reference: https://docs.confluent.io/confluent-cli/current/command-reference/flink/connection/confluent_flink_connection_create.html


## Pinecone

```bash
confluent flink connection create pinecone-connection --environment your-confluent-environment-id \
--cloud AWS \
--region us-east-1 \
--type pinecone \
--endpoint https://change-with-your-pinecone-endpoint.pinecone.io/query \
--api-key change-with-your-pinecone-key
```

## OpenAI embedding

```bash
confluent flink connection create openai-connection-vector-embeddings \
--environment your-confluent-environment-id \
--cloud AWS \
--region us-east-1 \
--type openai \
--endpoint https://api.openai.com/v1/embeddings \
--api-key change-with-your-open-ai-key
```

## OpenAI completions

```bash
confluent flink connection create openai-connection-completions \
--cloud AWS \
--region us-east-1 \
--environment your-confluent-environment-id \
--type openai \
--endpoint https://api.openai.com/v1/chat/completions \
--api-key change-with-your-open-ai-key
```
295 changes: 295 additions & 0 deletions simple-agentic-flow-flink-sql/2-create-tables-and-models.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,295 @@
------ ##### SETTING UP TABLES ##### -------

---------------------------------- INPUT TOPIC -------------------------------------------------------------------------------------------------------------

-------- Input table for customer messages from the chat

CREATE TABLE customer_message
(
conversation_id STRING NOT NULL,
customer_id STRING NOT NULL,
cusomer_message STRING NOT NULL,
`timestamp` BIGINT NOT NULL
);

---------------------------------- EMBEDDINGS MODEL and TOPICS --------------------------------------------------------------------------------------------------

-------- Add embedding model

CREATE
MODEL openai_embeddings
INPUT (input STRING)
OUTPUT (embedding ARRAY<FLOAT>)
WITH(
'task' = 'embedding',
'provider'= 'openai',
'openai.input_format'='OPENAI-EMBED',
'openai.model_version'='text-embedding-3-small',
'openai.connection' = 'openai-connection-vector-embeddings' );

CREATE TABLE vector_store_docs
(
text STRING,
embedding ARRAY<FLOAT>
) WITH (
'connector' = 'pinecone',
'pinecone.connection' = 'pinecone-pet-care-connection',
'pinecone.embedding_column' = 'embedding'
);

CREATE TABLE customer_message_and_embedding
(
conversation_id STRING NOT NULL,
customer_id STRING NOT NULL,
cusomer_message STRING,
`timestamp` BIGINT NOT NULL,
embedding array<float>
);

CREATE FUNCTION CLEAN_PINCONE_VECTOR_RESULT AS 'com.example.my.CleanPineConeVectorResult'
USING JAR 'confluent-artifact://cfa-0x3jq6';

CREATE TABLE customer_message_and_resources
(
conversation_id STRING NOT NULL,
customer_id STRING NOT NULL,
cusomer_message STRING,
`timestamp` BIGINT NOT NULL,
relevant_documentation STRING NOT NULL
);


---------------------------------- CUSTOMER INFO & MONGODB --------------------------------------------------------------------------------------------------

-------- Get info from MongoDB
SELECT *
from `mongo.pet-care.customer_info`

SELECT customer_id, pet_name
from `mongo.pet-care.customer_info`
DROP TABLE customer_message_and_customer_info
CREATE TABLE customer_message_and_customer_info
(
conversation_id STRING NOT NULL,
customer_id STRING NOT NULL,
cusomer_message STRING,
`timestamp` BIGINT NOT NULL,
relevant_documentation STRING NOT NULL,
pet_name STRING,
customer_email STRING,
pet_birthdate STRING,
pet_gender STRING,
allergies STRING,
pet_type STRING
);

---------------------------------- LLM ------------------------------

CREATE
MODEL helpful_chatbot
INPUT(text STRING)
OUTPUT(chatbot_response STRING)
COMMENT 'chatbot based on openai gpt 3.5 turbo'
WITH (
'provider' = 'openai',
'task' = 'text_generation',
'openai.connection' = 'openai-connection-completions',
'openai.model_version' = 'gpt-3.5-turbo',
'openai.system_prompt' =
'You are a helpful agent that has deep knowledge of pet care. Answer the question from the customer based on the information about customer pet and provided piece of documentation. Give your extended advice on the issue. Talk about the pet using its name. Be friendly and respectful');

CREATE TABLE chatbot_output
(
conversation_id STRING NOT NULL,
customer_id STRING NOT NULL,
cusomer_message STRING NOT NULL,
chatbot_response STRING
);

------ ##### Adding triage step ##### ------

CREATE
MODEL initial_triage
INPUT(text STRING)
OUTPUT(triage_result STRING)
COMMENT 'chatbot based on openai gpt 3.5 turbo'
WITH (
'provider' = 'openai',
'task' = 'text_generation',
'openai.connection' = 'openai-connection-completions',
'openai.model_version' = 'gpt-3.5-turbo',
'openai.system_prompt' =
'You are an agent that triages customer requests. Customers can do the following actions:
- GET_INFO,
- SCHEDULE_VET,
- BOOK_GROOMING,
- RAISE_ISSUE.

- GET_INFO is for any questions related to pet management and health that does not require vet involvement;
- SCHEDULE_VET - when there is a need based on your analysis;
- BOOK_GROOMING - when customer asks for a grooming appointment;
- RAISE_ISSUE - for issues related to services provided by the company.

YOUR TASK: based on content of customer message return ONE action type that fits best: GET_INFO, SCHEDULE_VET, BOOK_GROOMING or RAISE_ISSUE. DO NOT GIVE ANY OTHER INFORMATION, just type of the action.');

CREATE TABLE triage_output
(
conversation_id STRING NOT NULL,
customer_id STRING NOT NULL,
cusomer_message STRING NOT NULL,
`timestamp` BIGINT NOT NULL,
relevant_documentation STRING NOT NULL,
pet_name STRING,
customer_email STRING,
pet_birthdate STRING,
pet_gender STRING,
allergies STRING,
pet_type STRING,
triage_result STRING
);


INSERT INTO triage_output
SELECT conversation_id,
customer_id,
cusomer_message,
`timestamp`,
relevant_documentation,
pet_name,
customer_email,
pet_birthdate,
pet_gender,
allergies,
pet_type,
triage_result
FROM `customer_message_and_customer_info`,
LATERAL TABLE(ML_PREDICT('initial_triage',
CONCAT(
'This is customer information: they has a pet who is ', pet_type,
'. It is ', pet_gender,
'. It was born on ', pet_birthdate,
'. It has these allergies: ', allergies,
'. This is customer request: ',
cusomer_message,
'. This is relevant documentation: ',
relevant_documentation,
'YOUR TASK: return one of the following actions that best correspond to customer request: GET_INFO, SCHEDULE_VET, BOOK_GROOMING or RAISE_ISSUE'
)));

--------- Tables per task

CREATE TABLE recommendation_requests
(
conversation_id STRING NOT NULL,
customer_id STRING NOT NULL,
cusomer_message STRING NOT NULL,
`timestamp` BIGINT NOT NULL,
relevant_documentation STRING NOT NULL,
pet_name STRING,
customer_email STRING,
pet_birthdate STRING,
pet_gender STRING,
allergies STRING,
pet_type STRING,
);

CREATE TABLE schedule_vet_requests
(
conversation_id,
customer_id,
cusomer_message,
`timestamp`,
relevant_documentation,
pet_name,
customer_email,
pet_birthdate,
pet_gender,
allergies,
pet_type
);

CREATE TABLE book_grooming_requests
(
conversation_id,
customer_id,
cusomer_message,
`timestamp`,
relevant_documentation,
pet_name,
customer_email,
pet_birthdate,
pet_gender,
allergies,
pet_type
);

CREATE TABLE raise_issue_requests
(
conversation_id,
customer_id,
cusomer_message,
`timestamp`,
relevant_documentation,
pet_name,
customer_email,
pet_birthdate,
pet_gender,
allergies,
pet_type
);

--------- Recommendations

CREATE MODEL recommendation
INPUT(text STRING)
OUTPUT(recommendation STRING)
COMMENT 'chatbot based on openai gpt 3.5 turbo'
WITH (
'provider' = 'openai',
'task' = 'text_generation',
'openai.connection' = 'openai-connection-completions',
'openai.model_version' = 'gpt-3.5-turbo',
'openai.system_prompt' =
'You are a helpful agent that has deep knowledge of pet care. Answer the question or concern from the customer based on the information about customer pet and provided piece of documentation. Give your extended advice on the issue. Talk about the pet using its name. Be friendly and respectful');


CREATE TABLE recommendations_output(
conversation_id STRING NOT NULL,
customer_id STRING NOT NULL,
cusomer_message STRING NOT NULL,
recommendation STRING
);

INSERT INTO `recommendations_output`
SELECT
conversation_id,
customer_id,
cusomer_message,
recommendation
FROM `customer_message_and_customer_info`,
LATERAL TABLE(ML_PREDICT('recommendation',
CONCAT(
'This is customer information. He has a pet who is ', pet_type,
'. It is ', pet_gender,
'. It was born on ', pet_birthdate,
'. It has these allergies: ', allergies,
'. This is customer question: ',
cusomer_message,
'. This is relevant documentation: ',
relevant_documentation
)));


--------- Schedule

CREATE MODEL schedule
INPUT(text STRING)
OUTPUT(recommendation STRING)
COMMENT 'chatbot based on openai gpt 3.5 turbo'
WITH (
'provider' = 'openai',
'task' = 'text_generation',
'openai.connection' = 'openai-connection-completions',
'openai.model_version' = 'gpt-3.5-turbo',
'openai.system_prompt' =
'You are a helpful agent that has deep knowledge of pet care. Answer the question or concern from the customer based on the information about customer pet and provided piece of documentation. Give your extended advice on the issue. Talk about the pet using its name. Be friendly and respectful');
Loading