Code Monkey home page Code Monkey logo

tripal's Introduction

TriPal

TriPal is an AI service that proposes travel plans and provides reservation proposals for accommodation facilities in the form of conversations.

English | 日本語

Tech Stack

Installation

※ If using Docker as described in Usage②, only Step0 is necessary.

Step0: Clone this repository

$ git clone https://github.com/KorRyu3/TriPal
$ cd TriPal

Step1: Make Virtual Environment

$ python3 -m venv .venv

# Activate the virtual environment
## Windows
> .\.venv\Scripts\activate
## MacOS or Linux
$ source .venv/bin/activate

If you want to deactivate the virtual environment

$ deactivate

Step2: Install Requirements

$ pip install -r requirements.txt

Usage

There are two ways to run the app: with a local Python environment and with Docker.

Usage①: Run with Local Python Environment

Step0: Activate Environment

# Windows
> .\.venv\Scripts\activate
## MacOS or Linux
$ source .venv/bin/activate

Step1: Run App

$ python3 src/app.py

Step2: Access to the App

Open your browser and access to http://127.0.0.1:8000/.

Usage②: Run with Docker

Step0: Build and Run Docker Container

$ docker-compose up --build

Step1: Access to the App

Open your browser and access to http://127.0.0.1:8000/.

Directory Structure

TriPal
├── .dockerignore
├── .gitignore
├── CONTRIBUTING.md
├── docker-compose.yml
├── Dockerfile
├── README.md
├── README_jp.md
├── requirements.txt
├── .github
│   └── ...
├── .venv
│   └── ...
├── drawio
│   ├── architecture.drawio
│   ├── first_design.drawio
│   ├── log_ER.drawio
│   └── work-flow.drawio
└── src
    ├── __init__.py
    ├── .env
    ├── app.py
    ├── dalle3.py
    ├── llm_prompts.py
    ├── log_setup.py
    ├── tripalgpt.py
    ├── func_call_tools
    │   ├── reservations.py
    │   └── suggestions.py
    ├── logs
    │   ├── .gitingore
    │   └── ...
    ├── static
    │   ├── font
    │   │   └── ...
    │   ├── index.js
    │   └── style.css
    └── templates
        ├── index.html
        └── we-are-demo.html

Developers Information

About Environment Variables

The environment variables are managed by .env file. The file should be placed in src/ directory. Also, the contents of .env are as follows:

# .env

# Azure
# Azure OpenAI API
AZURE_OPENAI_API_DEPLOYMENT="<Deployment name>"
AZURE_OPENAI_API_KEY="<API key>"
AZURE_OPENAI_API_BASE="<Endpoint (base URL)>"
AZURE_OPENAI_API_VERSION="<Azure OpenAI's Version>"

# LangSmith
LANGCHAIN_API_KEY="<LangChain's API key>"
LANGCHAIN_TRACING_V2=true
LANGCHAIN_PROJECT="<Project name>"
LANGCHAIN_ENDPOINT="https://api.smith.langchain.com"

# TripAdvisor API
TRIPADVISOR_API_KEY="<Tripadvisor's API key>"

# Rakuten API
RAKUTEN_APPLICATION_ID="<Rakuten Developer's Application ID>"
RAKUTEN_AFFILIATE_ID="<Rakuten Developer's Affiliate ID>"

Contribution Guide

You can see the contribution guide in CONTRIBUTING.md.

Contributors

tripal's People

Contributors

korryu3 avatar nka21 avatar dependabot[bot] avatar

Stargazers

 avatar HiroM320 avatar

Watchers

 avatar

tripal's Issues

Azure Application Insightsにデバッグ用ログが送れない

Originally posted by @KorRyu3 in #30 (comment)

発端

エラーが起きた際のデバッグを楽にするために、Pythonの標準ライブラリであるloggingと、Azure Application Insights(以下、AAI)に接続できるハンドラーを使用してログを残そうとしていた。

バグについて

FastAPI内で、別モジュールで定義されているloggerを呼び出す関数を叩くとなぜかAAIにlogが送られない。
例)

# app.py
@app.get("/")
def index():
  log_info()  # この呼び出しで"log info"がAAIに送信されているはずだが、送信されない
  return

# sub.py
from opencensus.ext.azure.log_exporter import AzureLogHandler
logger = logging.getLogger(__name__)
logger.addHandler(AzureLogHandler())
def log_info():
  logger.info("log info")

バグの可能性(検証済みの事)

  1. そもそもAzureLogHandlerが送れていないのでは?
    A. いいえ。ログ自体は送れます。Azureでtripalのworkspaceのログを見ると確認できます。(AppTracesとAppExceptions)

  2. 別モジュールからloggerを呼び出すのがダメなのでは?
    A. いいえ。別モジュールで定義していても、きちんと呼び出せます。

  3. クラスに定義しているとlogger.infoは呼び出せないのでは?
    A. いいえ。きちんと呼び出せます。コンストラクタに定義しても正常に呼び出せました。

  4. FastAPIの中でloggerを呼び出すのがダメなのでは?
    A. いいえ。自分のモジュールに定義されているloggerは正常に動きます。

logger = logging.getLogger(__name__)
# app.py
@app.get("/")
def index():
logger.info("log info")  # これはきちんとAAIに送信される
  1. 別モジュールで定義しているloggerと自モジュールで定義されているloggerが混合してしまっているのでは?
    A. いいえ。正常にモジュール毎に定義されてます。ログのレベル別で確認した結果、きちんと別れてました。

  2. app.py噛ませずにtripalgpt.py単体で動かしたらどうなる?
    A. 正常に動作して、全てきちんと送信されます。こうなるとFastAPIに問題があるとしか思えません。

  3. logレベルが間違ってるんじゃ?
    A. いいえ。全てINFO以上で実験しているため正常です。

  4. WebSocketが悪いんじゃね?
    A. いいえ。"/"でも動作しないので確率は低いかと。

  5. logging.basicConfig(handlers=[AzureLogHandler()])とやれば解決できると記述があるが?
    A. 関数外だと普通に送信できますが、FastAPIのデコレータ(@appが上についてる関数)の中ではなぜかAAIに送信されませんでした。なんで????

  6. app.pyで定義したloggerをすべてのモジュールでインポートして使い回せばいいのでは?
    A. 出来るけど汚いから正直やりたくない... 手段の一つとしてはあり。やるならファイルハンドラーは別のloggerを定義する。

  7. 別モジュールのloggerをFastAPI内で動かそうとすると送れない!
    A. 多分これが原因?

Desktop (please complete the following information):

  • OS: MacBook Air
  • Browser: Edge
  • Version: 14.3

その他

10に書いてある妥協以外に方法が思いつかない。
↑ 無理だった。

本当に意味不明なバグ。
エラーが出ないバグが一番困る。

LLMが回答し始めたら、ユーザーがスクロールに絶対逆らえない

Originally posted by @KorRyu3 in #25 (comment)

問題点

LLMが回答し始めると、自動スクロールが始まる。
しかし、それが始まるとユーザーが自由にスクロールできなくなる。

理想の動作

  • LLMから回答が来た時、画面の一番下らへんに行ったら、自動でスクロールされる。
  • ただし、ユーザーはいつでもその自動スクロールから抜け出せて、自由に移動可能

デバッグ時の謎タイムアウトエラーについて

Originally posted by @nka21 in #25 (comment)


@nka21
デバッグしてたら、急にタイムアウトしたのでメモ

ConnectionResetError: [Errno 54] Connection reset by peer

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/kojimanaoki/Documents/GitHub/TriPal/.venv/lib/python3.11/site-packages/httpcore/_exceptions.py", line 10, in map_exceptions
    yield
  File "/Users/kojimanaoki/Documents/GitHub/TriPal/.venv/lib/python3.11/site-packages/httpcore/_backends/anyio.py", line 34, in read
    return await self._stream.receive(max_bytes=max_bytes)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kojimanaoki/Documents/GitHub/TriPal/.venv/lib/python3.11/site-packages/anyio/streams/tls.py", line 196, in receive
    data = await self._call_sslobject_method(self._ssl_object.read, max_bytes)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kojimanaoki/Documents/GitHub/TriPal/.venv/lib/python3.11/site-packages/anyio/streams/tls.py", line 138, in _call_sslobject_method
    data = await self.transport_stream.receive()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kojimanaoki/Documents/GitHub/TriPal/.venv/lib/python3.11/site-packages/anyio/_backends/_asyncio.py", line 1212, in receive
    raise self._protocol.exception
anyio.BrokenResourceError

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/kojimanaoki/Documents/GitHub/TriPal/.venv/lib/python3.11/site-packages/httpx/_transports/default.py", line 67, in map_httpcore_exceptions
    yield
  File "/Users/kojimanaoki/Documents/GitHub/TriPal/.venv/lib/python3.11/site-packages/httpx/_transports/default.py", line 252, in __aiter__
    async for part in self._httpcore_stream:
  File "/Users/kojimanaoki/Documents/GitHub/TriPal/.venv/lib/python3.11/site-packages/httpcore/_async/connection_pool.py", line 361, in __aiter__
    async for part in self._stream:
  File "/Users/kojimanaoki/Documents/GitHub/TriPal/.venv/lib/python3.11/site-packages/httpcore/_async/http11.py", line 337, in __aiter__
    raise exc
  File "/Users/kojimanaoki/Documents/GitHub/TriPal/.venv/lib/python3.11/site-packages/httpcore/_async/http11.py", line 329, in __aiter__
    async for chunk in self._connection._receive_response_body(**kwargs):
  File "/Users/kojimanaoki/Documents/GitHub/TriPal/.venv/lib/python3.11/site-packages/httpcore/_async/http11.py", line 198, in _receive_response_body
    event = await self._receive_event(timeout=timeout)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kojimanaoki/Documents/GitHub/TriPal/.venv/lib/python3.11/site-packages/httpcore/_async/http11.py", line 212, in _receive_event
    data = await self._network_stream.read(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kojimanaoki/Documents/GitHub/TriPal/.venv/lib/python3.11/site-packages/httpcore/_backends/anyio.py", line 31, in read
    with map_exceptions(exc_map):
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/contextlib.py", line 155, in __exit__
    self.gen.throw(typ, value, traceback)
  File "/Users/kojimanaoki/Documents/GitHub/TriPal/.venv/lib/python3.11/site-packages/httpcore/_exceptions.py", line 14, in map_exceptions
    raise to_exc(exc) from exc
httpcore.ReadError

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/kojimanaoki/Documents/GitHub/TriPal/.venv/lib/python3.11/site-packages/uvicorn/protocols/websockets/websockets_impl.py", line 250, in run_asgi
    result = await self.app(self.scope, self.asgi_receive, self.asgi_send)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kojimanaoki/Documents/GitHub/TriPal/.venv/lib/python3.11/site-packages/uvicorn/middleware/proxy_headers.py", line 84, in __call__
    return await self.app(scope, receive, send)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kojimanaoki/Documents/GitHub/TriPal/.venv/lib/python3.11/site-packages/fastapi/applications.py", line 1054, in __call__
    await super().__call__(scope, receive, send)
  File "/Users/kojimanaoki/Documents/GitHub/TriPal/.venv/lib/python3.11/site-packages/starlette/applications.py", line 116, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/Users/kojimanaoki/Documents/GitHub/TriPal/.venv/lib/python3.11/site-packages/starlette/middleware/errors.py", line 151, in __call__
    await self.app(scope, receive, send)
  File "/Users/kojimanaoki/Documents/GitHub/TriPal/.venv/lib/python3.11/site-packages/starlette/middleware/exceptions.py", line 62, in __call__
    await wrap_app_handling_exceptions(self.app, conn)(scope, receive, send)
  File "/Users/kojimanaoki/Documents/GitHub/TriPal/.venv/lib/python3.11/site-packages/starlette/_exception_handler.py", line 55, in wrapped_app
    raise exc
  File "/Users/kojimanaoki/Documents/GitHub/TriPal/.venv/lib/python3.11/site-packages/starlette/_exception_handler.py", line 44, in wrapped_app
    await app(scope, receive, sender)
  File "/Users/kojimanaoki/Documents/GitHub/TriPal/.venv/lib/python3.11/site-packages/starlette/routing.py", line 746, in __call__
    await route.handle(scope, receive, send)
  File "/Users/kojimanaoki/Documents/GitHub/TriPal/.venv/lib/python3.11/site-packages/starlette/routing.py", line 357, in handle
    await self.app(scope, receive, send)
  File "/Users/kojimanaoki/Documents/GitHub/TriPal/.venv/lib/python3.11/site-packages/starlette/routing.py", line 94, in app
    await wrap_app_handling_exceptions(app, session)(scope, receive, send)
  File "/Users/kojimanaoki/Documents/GitHub/TriPal/.venv/lib/python3.11/site-packages/starlette/_exception_handler.py", line 55, in wrapped_app
    raise exc
  File "/Users/kojimanaoki/Documents/GitHub/TriPal/.venv/lib/python3.11/site-packages/starlette/_exception_handler.py", line 44, in wrapped_app
    await app(scope, receive, sender)
  File "/Users/kojimanaoki/Documents/GitHub/TriPal/.venv/lib/python3.11/site-packages/starlette/routing.py", line 92, in app
    await func(session)
  File "/Users/kojimanaoki/Documents/GitHub/TriPal/.venv/lib/python3.11/site-packages/fastapi/routing.py", line 360, in app
    await dependant.call(**values)
  File "/Users/kojimanaoki/Documents/GitHub/TriPal/src/app.py", line 50, in chat
    async for output in tripal_gpt.get_async_generator_output(user_input=user_chat):
  File "/Users/kojimanaoki/Documents/GitHub/TriPal/src/tripalgpt.py", line 202, in get_async_generator_output
    async for res in generator_response:
  File "/Users/kojimanaoki/Documents/GitHub/TriPal/.venv/lib/python3.11/site-packages/langchain_core/runnables/base.py", line 737, in astream_log
    await task
  File "/Users/kojimanaoki/Documents/GitHub/TriPal/.venv/lib/python3.11/site-packages/langchain_core/runnables/base.py", line 691, in consume_astream
    async for chunk in self.astream(input, config, **kwargs):
  File "/Users/kojimanaoki/Documents/GitHub/TriPal/.venv/lib/python3.11/site-packages/langchain/agents/agent.py", line 1472, in astream
    async for step in iterator:
  File "/Users/kojimanaoki/Documents/GitHub/TriPal/.venv/lib/python3.11/site-packages/langchain/agents/agent_iterator.py", line 240, in __aiter__
    async for chunk in self.agent_executor._aiter_next_step(
  File "/Users/kojimanaoki/Documents/GitHub/TriPal/.venv/lib/python3.11/site-packages/langchain/agents/agent.py", line 1192, in _aiter_next_step
    output = await self.agent.aplan(
             ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kojimanaoki/Documents/GitHub/TriPal/.venv/lib/python3.11/site-packages/langchain/agents/agent.py", line 409, in aplan
    output = await self.runnable.ainvoke(inputs, config={"callbacks": callbacks})
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kojimanaoki/Documents/GitHub/TriPal/.venv/lib/python3.11/site-packages/langchain_core/runnables/base.py", line 1747, in ainvoke
    input = await step.ainvoke(
            ^^^^^^^^^^^^^^^^^^^
  File "/Users/kojimanaoki/Documents/GitHub/TriPal/.venv/lib/python3.11/site-packages/langchain_core/runnables/base.py", line 3422, in ainvoke
    return await self.bound.ainvoke(
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kojimanaoki/Documents/GitHub/TriPal/.venv/lib/python3.11/site-packages/langchain_core/language_models/chat_models.py", line 184, in ainvoke
    llm_result = await self.agenerate_prompt(
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kojimanaoki/Documents/GitHub/TriPal/.venv/lib/python3.11/site-packages/langchain_core/language_models/chat_models.py", line 506, in agenerate_prompt
    return await self.agenerate(
           ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kojimanaoki/Documents/GitHub/TriPal/.venv/lib/python3.11/site-packages/langchain_core/language_models/chat_models.py", line 466, in agenerate
    raise exceptions[0]
  File "/Users/kojimanaoki/Documents/GitHub/TriPal/.venv/lib/python3.11/site-packages/langchain_core/language_models/chat_models.py", line 569, in _agenerate_with_cache
    return await self._agenerate(
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kojimanaoki/Documents/GitHub/TriPal/.venv/lib/python3.11/site-packages/langchain_community/chat_models/openai.py", line 518, in _agenerate
    return await agenerate_from_stream(stream_iter)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kojimanaoki/Documents/GitHub/TriPal/.venv/lib/python3.11/site-packages/langchain_core/language_models/chat_models.py", line 85, in agenerate_from_stream
    async for chunk in stream:
  File "/Users/kojimanaoki/Documents/GitHub/TriPal/.venv/lib/python3.11/site-packages/langchain_community/chat_models/openai.py", line 484, in _astream
    async for chunk in await acompletion_with_retry(
  File "/Users/kojimanaoki/Documents/GitHub/TriPal/.venv/lib/python3.11/site-packages/openai/_streaming.py", line 94, in __aiter__
    async for item in self._iterator:
  File "/Users/kojimanaoki/Documents/GitHub/TriPal/.venv/lib/python3.11/site-packages/openai/_streaming.py", line 107, in __stream__
    async for sse in iterator:
  File "/Users/kojimanaoki/Documents/GitHub/TriPal/.venv/lib/python3.11/site-packages/openai/_streaming.py", line 98, in _iter_events
    async for sse in self._decoder.aiter(self.response.aiter_lines()):
  File "/Users/kojimanaoki/Documents/GitHub/TriPal/.venv/lib/python3.11/site-packages/openai/_streaming.py", line 190, in aiter
    async for line in iterator:
  File "/Users/kojimanaoki/Documents/GitHub/TriPal/.venv/lib/python3.11/site-packages/httpx/_models.py", line 967, in aiter_lines
    async for text in self.aiter_text():
  File "/Users/kojimanaoki/Documents/GitHub/TriPal/.venv/lib/python3.11/site-packages/httpx/_models.py", line 954, in aiter_text
    async for byte_content in self.aiter_bytes():
  File "/Users/kojimanaoki/Documents/GitHub/TriPal/.venv/lib/python3.11/site-packages/httpx/_models.py", line 933, in aiter_bytes
    async for raw_bytes in self.aiter_raw():
  File "/Users/kojimanaoki/Documents/GitHub/TriPal/.venv/lib/python3.11/site-packages/httpx/_models.py", line 991, in aiter_raw
    async for raw_stream_bytes in self.stream:
  File "/Users/kojimanaoki/Documents/GitHub/TriPal/.venv/lib/python3.11/site-packages/httpx/_client.py", line 147, in __aiter__
    async for chunk in self._stream:
  File "/Users/kojimanaoki/Documents/GitHub/TriPal/.venv/lib/python3.11/site-packages/httpx/_transports/default.py", line 251, in __aiter__
    with map_httpcore_exceptions():
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/contextlib.py", line 155, in __exit__
    self.gen.throw(typ, value, traceback)
  File "/Users/kojimanaoki/Documents/GitHub/TriPal/.venv/lib/python3.11/site-packages/httpx/_transports/default.py", line 84, in map_httpcore_exceptions
    raise mapped_exc(message) from exc
httpx.ReadError

JSのWebSocketのURLを、開発時と運用時でいちいち切り替えるのが面倒くさい

解決したいこと

JSのnew WebSocket()で指定するURLを開発用と運用用でいちいち切り替えるのが面倒くさい。

やりたいこと

デベロップ時とデプロイ時でURLを自動的に切り替えれるようにしたい。

dev: localhost:8000
deploy: azureのやつ

解決案

localhost:8000で繋がらなかったら、例外をキャッチしてazureのやつで接続する。

その他

もうこのプロジェクトも終わりが近づいてるし、後回しでもだいじょーぶ

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.