Skip to content

Commit

Permalink
update with smart prompter interactor
Browse files Browse the repository at this point in the history
  • Loading branch information
leeduckgo committed Dec 18, 2023
1 parent 7dedb0b commit 6460c60
Show file tree
Hide file tree
Showing 7 changed files with 293 additions and 27 deletions.
Binary file added .swp
Binary file not shown.
6 changes: 5 additions & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ config :tai_shang_micro_faas_system,
did_testnet: "0xc71124a51e0d63cfc6eb04e690c39a4ea36774ed4df77c00f7cbcbc9d0505b2c",
api_key: System.get_env("API_KEY"),
admin_key: System.get_env("ADMIN_KEY"),
clipdrop_key: System.get_env("CLIPDROP_KEY")
clipdrop_key: System.get_env("CLIPDROP_KEY"),
smart_prompter_endpoint: System.get_env("SMART_PROMPTER_ENDPOINT"),
smart_prompter_acct: System.get_env("SMART_PROMPTER_ACCT"),
smart_prompter_pwd: System.get_env("SMART_PROMPTER_PWD")


config :cors_plug,
max_age: 2592000,
Expand Down
112 changes: 86 additions & 26 deletions lib/components/ex_http.ex
Original file line number Diff line number Diff line change
Expand Up @@ -38,31 +38,31 @@ defmodule Components.ExHttp do
{:error, "POST retires #{@retries} times and not success"}
end

def http_post(_url, _data, _, retries) when retries == 0 do
{:error, "POST retires #{@retries} times and not success"}
end

def http_post(url, data, heads, retries) do
body = Poison.encode!(data)

url
|> HTTPoison.post(
body,
# [{"User-Agent", @default_user_agent}, {"Content-Type", "text/plain"}]
heads,
hackney: [headers: [{"User-Agent", @default_user_agent}]]
)
|> handle_response()
|> case do
{:ok, body} ->
{:ok, body}
{:error, 404} ->
{:error, 404}
{:error, _} ->
Process.sleep(500)
http_post(url, data, heads, retries - 1)
end
end
# def http_post(_url, _data, _, retries) when retries == 0 do
# {:error, "POST retires #{@retries} times and not success"}
# end

# def http_post(url, data, heads, retries) do
# body = Poison.encode!(data)

# url
# |> HTTPoison.post(
# body,
# # [{"User-Agent", @default_user_agent}, {"Content-Type", "text/plain"}]
# heads,
# hackney: [headers: [{"User-Agent", @default_user_agent}]]
# )
# |> handle_response()
# |> case do
# {:ok, body} ->
# {:ok, body}
# {:error, 404} ->
# {:error, 404}
# {:error, _} ->
# Process.sleep(500)
# http_post(url, data, heads, retries - 1)
# end
# end

def http_post(url, data, retries) do
body = Poison.encode!(data)
Expand Down Expand Up @@ -90,7 +90,7 @@ defmodule Components.ExHttp do
when status_code in 200..299 do
case Poison.decode(body) do
{:ok, json_body} ->
{:ok, json_body}
{:ok, ExStructTranslator.to_atom_struct(json_body)}

{:error, payload} ->
Logger.error("Reason: #{inspect(payload)}")
Expand All @@ -112,4 +112,64 @@ defmodule Components.ExHttp do
Logger.error("Reason: other_error")
error
end

def http_get(_url, _token, retries) when retries == 0 do
{:error, "GET retires #{@retries} times and not success"}
end

def http_get(url, token, retries) do
url
|> HTTPoison.get(
[
{"User-Agent", @default_user_agent},
{"authorization", "Bearer #{token}"}
],
hackney: [
headers: [
{"User-Agent", @default_user_agent}
]
]
)
|> handle_response()
|> case do
{:ok, body} ->
{:ok, body}

{:error, 404} ->
{:error, 404}
{:error, _} ->
Process.sleep(500)
http_get(url, token, retries - 1)
end
end

def http_post(_url, _data, _token, retries) when retries == 0 do
{:error, "POST retires #{@retries} times and not success"}
end

def http_post(url, data, token, retries) do
body = Poison.encode!(data)
url
|> HTTPoison.post(
body,
[
{"User-Agent", @default_user_agent},
{"Content-Type", "application/json"},
{"authorization", "Bearer #{token}"}
],
hackney: [headers: [{"User-Agent", @default_user_agent}]]
)
|> handle_response()
|> case do
{:ok, body} ->
{:ok, body}
{:error, 404} ->
{:error, 404}
{:error, _} ->
Process.sleep(1000)
http_post(url, data, token, retries - 1)
end
end


end
186 changes: 186 additions & 0 deletions lib/components/smart_prompter_interactor.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
defmodule Components.SmartPrompterInteractor do
alias Components.ExHttp
@paths %{
user:
%{
register: "api/users/register",
login: "api/users/log_in",
get_current_user: "api/current_user"
},
chat:
%{
topics: "api/topics",
},
templates:
%{
create: "api/prompt_templates"
}
}
def register(username, password) do
# TODO.
end

# for test
def register(endpoint) do
body = %{
user: %{
email: Constants.smart_prompter_acct(),
password: Constants.smart_prompter_pwd()
}
}
path = "#{endpoint}/#{@paths.user.register}"
ExHttp.http_post(path, body)
end

def get_current_user(endpoint) do
# path = "#{endpoint}/#{@paths.user.get_current_user}"
# IO.puts inspect path
# token = get_session(endpoint)
# ExHttp.http_get(path, token, 3)
:smart_prompter_user_info
|> Process.whereis()
|> Agent.get(fn user_info -> user_info end)
end

def login(endpoint) do
body = %{
user: %{
email: Constants.smart_prompter_acct(),
password: Constants.smart_prompter_pwd()
}
}
path = "#{endpoint}/#{@paths.user.login}"
ExHttp.http_post(path, body)
end

def register_agent() do
{:ok, agent} = Agent.start_link fn -> [] end
Process.register(agent, :smart_prompter)
end

def register_agent(:smart_prompter_user_info) do
{:ok, agent} = Agent.start_link fn -> [] end
Process.register(agent, :smart_prompter_user_info)
end

def set_session(endpoint) do
{
:ok,
%{
email: email,
id: id,
token: the_token
} = the_user_info
} = login(endpoint)
pid = Process.whereis(:smart_prompter)
pid_user_info = Process.whereis(:smart_prompter_user_info)
if is_nil(pid) do
register_agent()
end

if is_nil(pid_user_info) do
register_agent(:smart_prompter_user_info)
end

pid = Process.whereis(:smart_prompter)
Agent.update(pid, fn token -> the_token end)


pid_user_info = Process.whereis(:smart_prompter_user_info)
Agent.update(pid_user_info, fn user_info -> the_user_info end)
end

def get_session(endpoint) do

:smart_prompter
|> Process.whereis()
|> Agent.get(fn token -> token end)
end

# +--------+
# | Topics |
# +--------+

def list_topics(endpoint) do
token = get_session(endpoint)
path = "#{endpoint}/#{@paths.chat.topics}"
ExHttp.http_get(path, token, 3)
end

def list_topics(endpoint, topic_id) do
token = get_session(endpoint)
path = "#{endpoint}/#{@paths.chat.topics}/#{topic_id}"
ExHttp.http_get(path, token, 3)
end

def create_topic(endpoint, content) do
token = get_session(endpoint)
path = "#{endpoint}/#{@paths.chat.topics}"
body =
%{
topic: %{
content: content
}
}
ExHttp.http_post(path, body, token, 3)
end

def create_topic(endpoint, content, prompt_template_id) do
token = get_session(endpoint)
path = "#{endpoint}/#{@paths.chat.topics}"
body =
%{
topic: %{
content: content,
prompt_template_id: prompt_template_id
}
}
ExHttp.http_post(path, body, token, 3)
end

# +-----------+
# | Templates |
# +-----------+

def create_template(endpoint, title, content) do
token = get_session(endpoint)
path = "#{endpoint}/#{@paths.templates.create}"

body =
%{
prompt_template: %{
title: title,
content: content
}
}
ExHttp.http_post(path, body, token, 3)
end

def list_template(endpoint) do
token = get_session(endpoint)
path = "#{endpoint}/#{@paths.templates.create}"
ExHttp.http_get(path, token, 3)
end

def list_template(endpoint, user_id) do
token = get_session(endpoint)
path = "#{endpoint}/#{@paths.templates.create}?user_id=#{user_id}"
ExHttp.http_get(path, token, 3)
end

def get_template(endpoint, template_id) do
token = get_session(endpoint)
path = "#{endpoint}/api/prompt_templates/#{template_id}"
ExHttp.http_get(path, token, 3)
end

# +--------------------+
# | Funcs about Prompt |
# +--------------------+

def impl_vars(prompt, vars) do
Enum.reduce(vars, prompt, fn {key, value}, acc ->
String.replace(acc, "{#{key}}", value)
end)
end
end
16 changes: 16 additions & 0 deletions lib/utils/constants.ex
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,20 @@ defmodule Constants do
:tai_shang_micro_faas_system
|> Application.fetch_env!(:clipdrop_key)
end

# +---------------------+
# | smart prompt things |
# +---------------------+

def smart_prompter_endpoint() do
Application.fetch_env!(:tai_shang_micro_faas_system, :smart_prompter_endpoint)
end

def smart_prompter_acct() do
Application.fetch_env!(:tai_shang_micro_faas_system, :smart_prompter_acct)
end

def smart_prompter_pwd() do
Application.fetch_env!(:tai_shang_micro_faas_system, :smart_prompter_pwd)
end
end
Binary file added priv/static/images/856ff5240f11e498d0d6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added priv/static/images/d28b865b4a2b53adf99a.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 6460c60

Please sign in to comment.