Skip to content

Commit

Permalink
feat!: use microgenerator (#239)
Browse files Browse the repository at this point in the history
See UPGRADING.md for a list of changes.
  • Loading branch information
busunkim96 authored and dandhlee committed Feb 6, 2023
1 parent 72c4c0e commit c4e2ecf
Show file tree
Hide file tree
Showing 12 changed files with 93 additions and 80 deletions.
15 changes: 9 additions & 6 deletions dialogflow/create_document_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import os
import uuid

import dialogflow_v2beta1 as dialogflow
from google.cloud import dialogflow_v2beta1
import pytest

import document_management
Expand All @@ -31,11 +31,11 @@
@pytest.fixture(scope="function", autouse=True)
def setup_teardown():
# Create a knowledge base to use in document management
client = dialogflow.KnowledgeBasesClient()
project_path = client.project_path(PROJECT_ID)
knowledge_base = dialogflow.types.KnowledgeBase(
client = dialogflow_v2beta1.KnowledgeBasesClient()
project_path = client.common_project_path(PROJECT_ID)
knowledge_base = dialogflow_v2beta1.KnowledgeBase(
display_name=KNOWLEDGE_BASE_NAME)
response = client.create_knowledge_base(project_path, knowledge_base)
response = client.create_knowledge_base(parent=project_path, knowledge_base=knowledge_base)
pytest.KNOWLEDGE_BASE_ID = response.name.split(
'/knowledgeBases/')[1].split('\n')[0]

Expand All @@ -44,7 +44,10 @@ def setup_teardown():
# Delete the created knowledge base
knowledge_base_path = client.knowledge_base_path(
PROJECT_ID, pytest.KNOWLEDGE_BASE_ID)
client.delete_knowledge_base(knowledge_base_path, force=True)
request = dialogflow_v2beta1.DeleteKnowledgeBaseRequest(
name=knowledge_base_path, force=True
)
client.delete_knowledge_base(request=request)


@pytest.mark.flaky(max_runs=3, min_passes=1)
Expand Down
6 changes: 3 additions & 3 deletions dialogflow/create_knowledge_base_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import os
import uuid

import dialogflow_v2beta1 as dialogflow
from google.cloud import dialogflow_v2beta1
import pytest

import knowledge_base_management
Expand All @@ -32,11 +32,11 @@ def teardown():
yield

# Delete the created knowledge base
client = dialogflow.KnowledgeBasesClient()
client = dialogflow_v2beta1.KnowledgeBasesClient()
assert pytest.KNOWLEDGE_BASE_ID is not None
knowledge_base_path = client.knowledge_base_path(
PROJECT_ID, pytest.KNOWLEDGE_BASE_ID)
client.delete_knowledge_base(knowledge_base_path)
client.delete_knowledge_base(name=knowledge_base_path)


def test_create_knowledge_base(capsys):
Expand Down
19 changes: 11 additions & 8 deletions dialogflow/detect_intent_audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ def detect_intent_audio(project_id, session_id, audio_file_path,
Using the same `session_id` between requests allows continuation
of the conversation."""
import dialogflow_v2 as dialogflow
from google.cloud import dialogflow

session_client = dialogflow.SessionsClient()

# Note: hard coding audio_encoding and sample_rate_hertz for simplicity.
audio_encoding = dialogflow.enums.AudioEncoding.AUDIO_ENCODING_LINEAR_16
audio_encoding = dialogflow.AudioEncoding.AUDIO_ENCODING_LINEAR_16
sample_rate_hertz = 16000

session = session_client.session_path(project_id, session_id)
Expand All @@ -51,14 +51,17 @@ def detect_intent_audio(project_id, session_id, audio_file_path,
with open(audio_file_path, 'rb') as audio_file:
input_audio = audio_file.read()

audio_config = dialogflow.types.InputAudioConfig(
audio_config = dialogflow.InputAudioConfig(
audio_encoding=audio_encoding, language_code=language_code,
sample_rate_hertz=sample_rate_hertz)
query_input = dialogflow.types.QueryInput(audio_config=audio_config)

response = session_client.detect_intent(
session=session, query_input=query_input,
input_audio=input_audio)
query_input = dialogflow.QueryInput(audio_config=audio_config)

request = dialogflow.DetectIntentRequest(
session=session,
query_input=query_input,
input_audio=input_audio,
)
response = session_client.detect_intent(request=request)

print('=' * 20)
print('Query text: {}'.format(response.query_result.query_text))
Expand Down
20 changes: 11 additions & 9 deletions dialogflow/detect_intent_knowledge.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,28 +40,30 @@ def detect_intent_knowledge(project_id, session_id, language_code,
knowledge_base_id: The Knowledge base's id to query against.
texts: A list of text queries to send.
"""
import dialogflow_v2beta1 as dialogflow
from google.cloud import dialogflow_v2beta1 as dialogflow
session_client = dialogflow.SessionsClient()

session_path = session_client.session_path(project_id, session_id)
print('Session path: {}\n'.format(session_path))

for text in texts:
text_input = dialogflow.types.TextInput(
text_input = dialogflow.TextInput(
text=text, language_code=language_code)

query_input = dialogflow.types.QueryInput(text=text_input)
query_input = dialogflow.QueryInput(text=text_input)

knowledge_base_path = dialogflow.knowledge_bases_client \
.KnowledgeBasesClient \
knowledge_base_path = dialogflow.KnowledgeBasesClient \
.knowledge_base_path(project_id, knowledge_base_id)

query_params = dialogflow.types.QueryParameters(
query_params = dialogflow.QueryParameters(
knowledge_base_names=[knowledge_base_path])

response = session_client.detect_intent(
session=session_path, query_input=query_input,
query_params=query_params)
request = dialogflow.DetectIntentRequest(
session=session_path,
query_input=query_input,
query_params=query_params
)
response = session_client.detect_intent(request=request)

print('=' * 20)
print('Query text: {}'.format(response.query_result.query_text))
Expand Down
14 changes: 7 additions & 7 deletions dialogflow/detect_intent_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,21 @@ def detect_intent_stream(project_id, session_id, audio_file_path,
Using the same `session_id` between requests allows continuation
of the conversation."""
import dialogflow_v2 as dialogflow
from google.cloud import dialogflow
session_client = dialogflow.SessionsClient()

# Note: hard coding audio_encoding and sample_rate_hertz for simplicity.
audio_encoding = dialogflow.enums.AudioEncoding.AUDIO_ENCODING_LINEAR_16
audio_encoding = dialogflow.AudioEncoding.AUDIO_ENCODING_LINEAR_16
sample_rate_hertz = 16000

session_path = session_client.session_path(project_id, session_id)
print('Session path: {}\n'.format(session_path))

def request_generator(audio_config, audio_file_path):
query_input = dialogflow.types.QueryInput(audio_config=audio_config)
query_input = dialogflow.QueryInput(audio_config=audio_config)

# The first request contains the configuration.
yield dialogflow.types.StreamingDetectIntentRequest(
yield dialogflow.StreamingDetectIntentRequest(
session=session_path, query_input=query_input)

# Here we are reading small chunks of audio data from a local
Expand All @@ -62,15 +62,15 @@ def request_generator(audio_config, audio_file_path):
if not chunk:
break
# The later requests contains audio data.
yield dialogflow.types.StreamingDetectIntentRequest(
yield dialogflow.StreamingDetectIntentRequest(
input_audio=chunk)

audio_config = dialogflow.types.InputAudioConfig(
audio_config = dialogflow.InputAudioConfig(
audio_encoding=audio_encoding, language_code=language_code,
sample_rate_hertz=sample_rate_hertz)

requests = request_generator(audio_config, audio_file_path)
responses = session_client.streaming_detect_intent(requests)
responses = session_client.streaming_detect_intent(requests=requests)

print('=' * 20)
for response in responses:
Expand Down
8 changes: 4 additions & 4 deletions dialogflow/detect_intent_texts.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,20 @@ def detect_intent_texts(project_id, session_id, texts, language_code):
Using the same `session_id` between requests allows continuation
of the conversation."""
import dialogflow_v2 as dialogflow
from google.cloud import dialogflow
session_client = dialogflow.SessionsClient()

session = session_client.session_path(project_id, session_id)
print('Session path: {}\n'.format(session))

for text in texts:
text_input = dialogflow.types.TextInput(
text_input = dialogflow.TextInput(
text=text, language_code=language_code)

query_input = dialogflow.types.QueryInput(text=text_input)
query_input = dialogflow.QueryInput(text=text_input)

response = session_client.detect_intent(
session=session, query_input=query_input)
request={'session': session, 'query_input': query_input})

print('=' * 20)
print('Query text: {}'.format(response.query_result.query_text))
Expand Down
13 changes: 6 additions & 7 deletions dialogflow/detect_intent_with_sentiment_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,29 +35,28 @@ def detect_intent_with_sentiment_analysis(project_id, session_id, texts,
Using the same `session_id` between requests allows continuation
of the conversation."""
import dialogflow_v2 as dialogflow
from google.cloud import dialogflow
session_client = dialogflow.SessionsClient()

session_path = session_client.session_path(project_id, session_id)
print('Session path: {}\n'.format(session_path))

for text in texts:
text_input = dialogflow.types.TextInput(
text_input = dialogflow.TextInput(
text=text, language_code=language_code)

query_input = dialogflow.types.QueryInput(text=text_input)
query_input = dialogflow.QueryInput(text=text_input)

# Enable sentiment analysis
sentiment_config = dialogflow.types.SentimentAnalysisRequestConfig(
sentiment_config = dialogflow.SentimentAnalysisRequestConfig(
analyze_query_text_sentiment=True)

# Set the query parameters with sentiment analysis
query_params = dialogflow.types.QueryParameters(
query_params = dialogflow.QueryParameters(
sentiment_analysis_request_config=sentiment_config)

response = session_client.detect_intent(
session=session_path, query_input=query_input,
query_params=query_params)
request={'session': session_path, 'query_input': query_input, 'query_params': query_params})

print('=' * 20)
print('Query text: {}'.format(response.query_result.query_text))
Expand Down
19 changes: 11 additions & 8 deletions dialogflow/detect_intent_with_texttospeech_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,29 @@ def detect_intent_with_texttospeech_response(project_id, session_id, texts,
Using the same `session_id` between requests allows continuation
of the conversation."""
import dialogflow_v2 as dialogflow
from google.cloud import dialogflow
session_client = dialogflow.SessionsClient()

session_path = session_client.session_path(project_id, session_id)
print('Session path: {}\n'.format(session_path))

for text in texts:
text_input = dialogflow.types.TextInput(
text_input = dialogflow.TextInput(
text=text, language_code=language_code)

query_input = dialogflow.types.QueryInput(text=text_input)
query_input = dialogflow.QueryInput(text=text_input)

# Set the query parameters with sentiment analysis
output_audio_config = dialogflow.types.OutputAudioConfig(
audio_encoding=dialogflow.enums.OutputAudioEncoding
output_audio_config = dialogflow.OutputAudioConfig(
audio_encoding=dialogflow.OutputAudioEncoding
.OUTPUT_AUDIO_ENCODING_LINEAR_16)

response = session_client.detect_intent(
session=session_path, query_input=query_input,
output_audio_config=output_audio_config)
request = dialogflow.DetectIntentRequest(
session=session_path,
query_input=query_input,
output_audio_config=output_audio_config
)
response = session_client.detect_intent(request=request)

print('=' * 20)
print('Query text: {}'.format(response.query_result.query_text))
Expand Down
13 changes: 7 additions & 6 deletions dialogflow/document_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,20 @@ def create_document(project_id, knowledge_base_id, display_name, mime_type,
EXTRACTIVE_QA.
content_uri: Uri of the document, e.g. gs://path/mydoc.csv,
http://mypage.com/faq.html."""
import dialogflow_v2beta1 as dialogflow
from google.cloud import dialogflow_v2beta1 as dialogflow
client = dialogflow.DocumentsClient()
knowledge_base_path = client.knowledge_base_path(project_id,
knowledge_base_id)
knowledge_base_path = dialogflow.KnowledgeBasesClient.knowledge_base_path(
project_id, knowledge_base_id)

document = dialogflow.types.Document(
document = dialogflow.Document(
display_name=display_name, mime_type=mime_type,
content_uri=content_uri)

document.knowledge_types.append(
dialogflow.types.Document.KnowledgeType.Value(knowledge_type))
getattr(dialogflow.Document.KnowledgeType, knowledge_type)
)

response = client.create_document(knowledge_base_path, document)
response = client.create_document(parent=knowledge_base_path, document=document)
print('Waiting for results...')
document = response.result(timeout=120)
print('Created Document:')
Expand Down
32 changes: 16 additions & 16 deletions dialogflow/intent_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@

# [START dialogflow_list_intents]
def list_intents(project_id):
import dialogflow_v2 as dialogflow
from google.cloud import dialogflow
intents_client = dialogflow.IntentsClient()

parent = intents_client.project_agent_path(project_id)
parent = dialogflow.AgentsClient.agent_path(project_id)

intents = intents_client.list_intents(parent)
intents = intents_client.list_intents(request={'parent': parent})

for intent in intents:
print('=' * 20)
Expand All @@ -63,27 +63,27 @@ def list_intents(project_id):
def create_intent(project_id, display_name, training_phrases_parts,
message_texts):
"""Create an intent of the given intent type."""
import dialogflow_v2 as dialogflow
from google.cloud import dialogflow
intents_client = dialogflow.IntentsClient()

parent = intents_client.project_agent_path(project_id)
parent = dialogflow.AgentsClient.agent_path(project_id)
training_phrases = []
for training_phrases_part in training_phrases_parts:
part = dialogflow.types.Intent.TrainingPhrase.Part(
part = dialogflow.Intent.TrainingPhrase.Part(
text=training_phrases_part)
# Here we create a new training phrase for each provided part.
training_phrase = dialogflow.types.Intent.TrainingPhrase(parts=[part])
training_phrase = dialogflow.Intent.TrainingPhrase(parts=[part])
training_phrases.append(training_phrase)

text = dialogflow.types.Intent.Message.Text(text=message_texts)
message = dialogflow.types.Intent.Message(text=text)
text = dialogflow.Intent.Message.Text(text=message_texts)
message = dialogflow.Intent.Message(text=text)

intent = dialogflow.types.Intent(
intent = dialogflow.Intent(
display_name=display_name,
training_phrases=training_phrases,
messages=[message])

response = intents_client.create_intent(parent, intent)
response = intents_client.create_intent(request={'parent': parent, 'intent': intent})

print('Intent created: {}'.format(response))
# [END dialogflow_create_intent]
Expand All @@ -92,22 +92,22 @@ def create_intent(project_id, display_name, training_phrases_parts,
# [START dialogflow_delete_intent]
def delete_intent(project_id, intent_id):
"""Delete intent with the given intent type and intent value."""
import dialogflow_v2 as dialogflow
from google.cloud import dialogflow
intents_client = dialogflow.IntentsClient()

intent_path = intents_client.intent_path(project_id, intent_id)

intents_client.delete_intent(intent_path)
intents_client.delete_intent(request={'name': intent_path})
# [END dialogflow_delete_intent]


# Helper to get intent from display name.
def _get_intent_ids(project_id, display_name):
import dialogflow_v2 as dialogflow
from google.cloud import dialogflow
intents_client = dialogflow.IntentsClient()

parent = intents_client.project_agent_path(project_id)
intents = intents_client.list_intents(parent)
parent = dialogflow.AgentsClient.agent_path(project_id)
intents = intents_client.list_intents(request={'parent': parent})
intent_names = [
intent.name for intent in intents
if intent.display_name == display_name]
Expand Down
3 changes: 1 addition & 2 deletions dialogflow/intent_management_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@

def test_create_intent(capsys):
intent_management.create_intent(
PROJECT_ID, INTENT_DISPLAY_NAME, TRAINING_PHRASE_PARTS,
MESSAGE_TEXTS)
PROJECT_ID, INTENT_DISPLAY_NAME, TRAINING_PHRASE_PARTS, MESSAGE_TEXTS)

intent_ids = intent_management._get_intent_ids(
PROJECT_ID, INTENT_DISPLAY_NAME)
Expand Down
Loading

0 comments on commit c4e2ecf

Please sign in to comment.