|
525 | 525 | "source": [
|
526 | 526 | "## Scores\n",
|
527 | 527 | "\n",
|
528 |
| - "[Scores](https://langfuse.com/docs/scores/overview) are used to evaluate single executions/traces. They can created manually via the Langfuse UI or via the SDKs.\n", |
| 528 | + "[Scores](https://langfuse.com/docs/scores/overview) are used to evaluate single executions/traces. They can be created via Annotation in the Langfuse UI or via the SDKs.\n", |
529 | 529 | "\n",
|
530 | 530 | "If the score relates to a specific step of the trace, specify the `observation_id`.\n",
|
531 | 531 | "\n",
|
|
704 | 704 | "langfuse.auth_check()"
|
705 | 705 | ]
|
706 | 706 | },
|
| 707 | + { |
| 708 | + "cell_type": "markdown", |
| 709 | + "metadata": {}, |
| 710 | + "source": [ |
| 711 | + "### Google Cloud Functions\n", |
| 712 | + "\n", |
| 713 | + "When using Langfuse in a Google Cloud Function or a Firebase Function, the underlying managed Python runtime has issues with threading whenever threads are spawned off the main scope and not inside the actual function scope. [See here](https://www.googlecloudcommunity.com/gc/Serverless/The-issue-with-pythons-s-threading-on-Google-Function/m-p/614384). Since Langfuse uses background threads to deliver the observability events, this will lead to incomplete traces.\n", |
| 714 | + "\n", |
| 715 | + "Make sure to initialize Langfuse always _inside_ the function body. If you want to reuse the created Langfuse clients in different modules, use lazy initialization of the Langfuse client to ensure the actual initialization occurs inside the function execution context.\n", |
| 716 | + "\n", |
| 717 | + "```python\n", |
| 718 | + "import functions_framework\n", |
| 719 | + "from langfuse import Langfuse\n", |
| 720 | + "\n", |
| 721 | + "\n", |
| 722 | + "# Lazy initialization of the Langfuse client to allow imports in other modules\n", |
| 723 | + "def get_langfuse():\n", |
| 724 | + " if not hasattr(get_langfuse, \"langfuse\"):\n", |
| 725 | + " get_langfuse.langfuse = Langfuse(debug=True)\n", |
| 726 | + "\n", |
| 727 | + " return get_langfuse.langfuse\n", |
| 728 | + "\n", |
| 729 | + "\n", |
| 730 | + "# Google Cloud Function\n", |
| 731 | + "@functions_framework.http\n", |
| 732 | + "def hello_world(request):\n", |
| 733 | + " langfuse = get_langfuse()\n", |
| 734 | + "\n", |
| 735 | + " response = \"Hello world!\"\n", |
| 736 | + " langfuse.trace(name=\"my-cloud-function\", output=response)\n", |
| 737 | + "\n", |
| 738 | + " langfuse.flush() # Ensure all events are sent before the function terminates\n", |
| 739 | + "\n", |
| 740 | + " return response\n", |
| 741 | + "```" |
| 742 | + ] |
| 743 | + }, |
707 | 744 | {
|
708 | 745 | "cell_type": "markdown",
|
709 | 746 | "metadata": {
|
|
0 commit comments