Skip to content

Commit

Permalink
Añadir frases de entrenamiento
Browse files Browse the repository at this point in the history
  • Loading branch information
alanraul authored Jul 25, 2018
2 parents ee1792c + 22b3256 commit 0d36ae5
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 60 deletions.
2 changes: 2 additions & 0 deletions .env.dist
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
DIALOGFLOW_URL=
DIALOGFLOW_PROJECT_ID=

GOOGLE_CREDENTIALS=
CLIENT_EMAIL=
76 changes: 74 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,86 @@
# Flowex

**No oficial Elixir sdk para Dialogflow**
Cliente de Elixir para el Dialogflow API v2.

## Instalación

Primero, agrega Flowex a su tus dependencias en mix.exs:

```elixir
def deps do
[
{:flowex, "~> 0.1.0"}
{:flowex, "~> 1.0.0"}
]
end
```

### Configurar variables de entorno.

El archivo __.env.dist__ contiene un listado actualizado de las variables de entorno necesarias para el proyecto, se debe copiar ese archivo a uno nuevo llamado __.env__

Tambien necesitaras crear un archivo llamando google_credentials.json dentro de la carpeta secrets.

Preguntar al equipo por los valores de las variables de entorno.

Exporta las variables

```shell
export $(cat .env | xargs)
```

## Como contribuir.

Pasos para contribuir en el proyecto:

- Hacer un __fork__ del repositorio a nuestra cuenta privada de Github.
- Clonar nuestro __fork__ en nuestra maquina de trabajo.
- Crear un remote llamado upstream que apunte hacia el repo de Resuelve.

```shell
git remote add upstream git@github.com:resuelve/flowex.git
```

- Lee las [guías de desarrollo.](https://github.com/resuelve/guias-desarrollo)

## Instalar dependencias

```shell
mix deps.get
```
## Uso

### Flowex.Service.Agent

Obtiene el agente al que está asociado el project_id. [(📘)](https://dialogflow.com/docs/reference/api-v2/rest/v2/projects/getAgent)

```elixir
get()
```

### Flowex.Service.Intents

Lista los de intents de un agente. [(📘)](https://dialogflow.com/docs/reference/api-v2/rest/v2/projects.agent.intents/list)

```elixir
list(language \\ "es", view \\ "INTENT_VIEW_UNSPECIFIED", pageSize \\ 100, token \\ nil)
```

Obtiene un intent buscando por id. [(📘)](https://dialogflow.com/docs/reference/api-v2/rest/v2/projects.agent.intents/get)

```elixir
get(id, languageCode \\ "es", intentView \\ "INTENT_VIEW_UNSPECIFIED")
```

Añade un frase de entrenamiento a un intent. [(📘)](https://dialogflow.com/docs/reference/api-v2/rest/v2/projects.agent.intents/patch)

```elixir
add_training_phrase(id, language \\ "es", text)
```

### Flowex.Service.Sessions

Procesa una consulta en lenguaje natural para detectar un intent con la respuesta apropiada [(📘)](https://dialogflow.com/docs/reference/api-v2/rest/v2/projects.agent.sessions/detectIntent)

```elixir
get()
```
2 changes: 1 addition & 1 deletion lib/flowex/service/agent.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule Flowex.Service.Agent do
alias Flowex

@doc """
Recupera el agente que está asociado al project_id.
Obtiene el agente al que está asociado el project_id.
"""
@spec get() :: tuple
def get do
Expand Down
39 changes: 32 additions & 7 deletions lib/flowex/service/intents.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,43 @@ defmodule Flowex.Service.Intents do
alias Flowex

@doc """
Lista de intents.
Lista los de intents de un agente.
"""
@spec list() :: tuple
def list do
Flowex.request(:get, "intents", "")
@spec list(String.t, String.t, integer, String.t | nil) :: tuple
def list(language \\ "es", view \\ "INTENT_VIEW_UNSPECIFIED", pageSize \\ 1, token \\ nil) do
url = "intents?languageCode=#{language}&intentView=#{view}&pageSize=#{pageSize}&pageToken=#{token}"
Flowex.request(:get, url, "")
end

@doc """
Obtiene un intent buscando por id.
"""
@spec get(String.t) :: tuple
def get(id) do
Flowex.request(:get, "intents/#{id}", "")
@spec get(String.t, String.t, String.t) :: tuple
def get(id, language \\ "es", view \\ "INTENT_VIEW_UNSPECIFIED") do
url = "intents/#{id}?languageCode=#{language}&intentView=#{view}"

Flowex.request(:get, url, "")
end

@doc """
Añade un frase de entrenamiento a un intent.
"""
@spec add_training_phrase(String.t, String.t, String.t) :: tuple
def add_training_phrase(id, text, language \\ "es") do
url = "intents/#{id}?languageCode=#{language}&intentView=INTENT_VIEW_FULL"

{:ok, intent} = get(id, language, "INTENT_VIEW_FULL")

intent =
[%{
"name" => UUID.uuid4(),
"parts" => [%{"text" => text}],
"type" => "EXAMPLE"
}]
|> Enum.concat(intent["trainingPhrases"])
|> (&Map.put(intent, "trainingPhrases", &1)).()
|> Poison.encode!

Flowex.request(:patch, url, intent)
end
end
11 changes: 5 additions & 6 deletions lib/flowex/service/sessions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,22 @@ defmodule Flowex.Service.Sessions do
alias Flowex

@doc """
Procesa una consulta en lenguaje natural y devuelve datos
estructurados y procesables como resultado.
Procesa una consulta en lenguaje natural para detectar un intent con la respuesta
apropiada.
"""
@spec detect_intent(String.t, String.t) :: tuple
def detect_intent(text, session_id) do
@spec detect_intent(String.t, String.t, String.t) :: tuple
def detect_intent(text, session_id, language \\ "es") do
body =
%{
queryInput: %{
text: %{
text: text,
languageCode: "es"
languageCode: language
}
}
}
|> Poison.encode!

Flowex.request(:post, "sessions/#{session_id}:detectIntent", body)
end

end
7 changes: 4 additions & 3 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Flowex.Mixfile do
def project do
[
app: :flowex,
version: "0.1.0",
version: "1.0.0",
elixir: "~> 1.5",
start_permanent: Mix.env == :prod,
deps: deps(),
Expand All @@ -24,11 +24,12 @@ defmodule Flowex.Mixfile do
defp deps do
[
{:credo, "~> 0.8", only: [:dev, :test]},
{:elixir_uuid, "~> 1.2" },
{:excoveralls, "~> 0.7", only: :test},
{:goth, "~> 0.9.0"},
{:httpoison, "~> 0.13"},
{:mock, "~> 0.3.0", only: :test},
{:poison, "~> 3.1"},
{:excoveralls, "~> 0.7", only: :test},
{:goth, "~> 0.9.0"}
]
end
end
1 change: 1 addition & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"bunt": {:hex, :bunt, "0.2.0", "951c6e801e8b1d2cbe58ebbd3e616a869061ddadcc4863d0a2182541acae9a38", [:mix], [], "hexpm"},
"certifi": {:hex, :certifi, "2.3.1", "d0f424232390bf47d82da8478022301c561cf6445b5b5fb6a84d49a9e76d2639", [:rebar3], [{:parse_trans, "3.2.0", [hex: :parse_trans, repo: "hexpm", optional: false]}], "hexpm"},
"credo": {:hex, :credo, "0.9.3", "76fa3e9e497ab282e0cf64b98a624aa11da702854c52c82db1bf24e54ab7c97a", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:poison, ">= 0.0.0", [hex: :poison, repo: "hexpm", optional: false]}], "hexpm"},
"elixir_uuid": {:hex, :elixir_uuid, "1.2.0", "ff26e938f95830b1db152cb6e594d711c10c02c6391236900ddd070a6b01271d", [:mix], [], "hexpm"},
"excoveralls": {:hex, :excoveralls, "0.8.2", "b941a08a1842d7aa629e0bbc969186a4cefdd035bad9fe15d43aaaaaeb8fae36", [:mix], [{:exjsx, ">= 3.0.0", [hex: :exjsx, repo: "hexpm", optional: false]}, {:hackney, ">= 0.12.0", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm"},
"exjsx": {:hex, :exjsx, "4.0.0", "60548841e0212df401e38e63c0078ec57b33e7ea49b032c796ccad8cde794b5c", [:mix], [{:jsx, "~> 2.8.0", [hex: :jsx, repo: "hexpm", optional: false]}], "hexpm"},
"goth": {:hex, :goth, "0.9.0", "92b30293623d8bd970693ec2aa497651e02542f6fb760d2eb49f30ff08346f59", [:mix], [{:httpoison, "~> 0.11 or ~> 1.0", [hex: :httpoison, repo: "hexpm", optional: false]}, {:json_web_token, "~> 0.2.10", [hex: :json_web_token, repo: "hexpm", optional: false]}, {:poison, "~> 2.1 or ~> 3.0", [hex: :poison, repo: "hexpm", optional: false]}], "hexpm"},
Expand Down
Empty file added secrets/.keep
Empty file.
1 change: 1 addition & 0 deletions test/service/agent_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ defmodule Flowex.Service.AgentTest do
alias Goth.Token

@agent %{
"avatarUri" => "https://storage.googleapis.com/l_bot.png",
"classificationThreshold" => 0.3,
"defaultLanguageCode" => "es",
"description" => "Agente de pruebas",
Expand Down
51 changes: 10 additions & 41 deletions test/service/intents_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,54 +9,23 @@ defmodule Flowex.Service.IntentsTest do
@intents %{
"intents" => [
%{
"action" => "input.welcome",
"displayName" => "Default Welcome Intent",
"events" => ["WELCOME"],
"messages" => [
%{"text" => %{"text" => ["¡Hola!", "¡Hey!", "¡Buenos días!"]}}
],
"name" => "projects/lbot-170189/agent/intents/225726d5-94ec-481b-b8ef-d7360e9dfc38",
"priority" => 500000
},
%{
"action" => "input.unknown",
"displayName" => "Default Fallback Intent",
"isFallback" => true,
"messages" => [
%{
"text" => %{
"text" => ["Ups, no he entendido a que te refieres.",
"¿Podrías repetirlo, por favor?", "¿Disculpa?", "¿Decías?",
"¿Cómo?"]
}
}
],
"name" => "projects/lbot-170189/agent/intents/5eec5344-8a09-40ba-8f46-1d2ed3f7b0df",
"displayName" => "Acción (Despedida)",
"messages" => [%{"text" => %{"text" => ["Bye", "Nos vemos"]}}],
"name" => "projects/lbot-170189/agent/intents/4cff39af-ba13-4a62-ba6f-8a79f4f5b324",
"priority" => 500000
}
]
],
"nextPageToken" => "0xFAKETOKEN_Q="
}

@intent %{
"action" => "input.unknown",
"displayName" => "Default Fallback Intent",
"isFallback" => true,
"messages" => [
%{
"text" => %{
"text" => [
"Ups, no he entendido a que te refieres.",
"¿Podrías repetirlo, por favor?", "¿Disculpa?",
"¿Decías?", "¿Cómo?"
]
}
}
],
"name" => "projects/lbot-170189/agent/intents/5eec5344-8a09-40ba-8f46-1d2ed3f7b0df",
"displayName" => "Acción (Despedida)",
"messages" => [%{"text" => %{"text" => ["Bye", "Nos vemos"]}}],
"name" => "projects/lbot-170189/agent/intents/4cff39af-ba13-4a62-ba6f-8a79f4f5b324",
"priority" => 500000
}

test "list/0 list all intents" do
test "list list all intents" do
with_mocks([
{
Token,
Expand All @@ -76,7 +45,7 @@ defmodule Flowex.Service.IntentsTest do
end
end

test "get/1 get an intent by id" do
test "get/3 get an intent by id" do
with_mocks([
{
Token,
Expand Down

0 comments on commit 0d36ae5

Please sign in to comment.