File size: 5,369 Bytes
447ebeb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "## Use LiteLLM with Langfuse\n",
        "https://docs.litellm.ai/docs/observability/langfuse_integration"
      ],
      "metadata": {
        "id": "4FbDOmcj2VkM"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Install Dependencies"
      ],
      "metadata": {
        "id": "21W8Woog26Ns"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "!pip install litellm langfuse"
      ],
      "metadata": {
        "id": "xrjKLBxhxu2L"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Set Env Variables"
      ],
      "metadata": {
        "id": "jHEu-TjZ29PJ"
      }
    },
    {
      "cell_type": "code",
      "execution_count": 10,
      "metadata": {
        "id": "QWd9rTysxsWO"
      },
      "outputs": [],
      "source": [
        "import litellm\n",
        "from litellm import completion\n",
        "import os\n",
        "\n",
        "# from https://cloud.langfuse.com/\n",
        "os.environ[\"LANGFUSE_PUBLIC_KEY\"] = \"\"\n",
        "os.environ[\"LANGFUSE_SECRET_KEY\"] = \"\"\n",
        "\n",
        "\n",
        "# OpenAI and Cohere keys\n",
        "# You can use any of the litellm supported providers: https://docs.litellm.ai/docs/providers\n",
        "os.environ['OPENAI_API_KEY']=\"\"\n",
        "os.environ['COHERE_API_KEY']=\"\"\n"
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Set LangFuse as a callback for sending data\n",
        "## OpenAI completion call"
      ],
      "metadata": {
        "id": "NodQl0hp3Lma"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# set langfuse as a callback, litellm will send the data to langfuse\n",
        "litellm.success_callback = [\"langfuse\"]\n",
        "\n",
        "# openai call\n",
        "response = completion(\n",
        "  model=\"gpt-3.5-turbo\",\n",
        "  messages=[\n",
        "    {\"role\": \"user\", \"content\": \"Hi πŸ‘‹ - i'm openai\"}\n",
        "  ]\n",
        ")\n",
        "\n",
        "print(response)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "vNAuwJY1yp_F",
        "outputId": "c3a71e26-13f5-4379-fac9-409290ba79bb"
      },
      "execution_count": 8,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "{\n",
            "  \"id\": \"chatcmpl-85nP4xHdAP3jAcGneIguWATS9qdoO\",\n",
            "  \"object\": \"chat.completion\",\n",
            "  \"created\": 1696392238,\n",
            "  \"model\": \"gpt-3.5-turbo-0613\",\n",
            "  \"choices\": [\n",
            "    {\n",
            "      \"index\": 0,\n",
            "      \"message\": {\n",
            "        \"role\": \"assistant\",\n",
            "        \"content\": \"Hello! How can I assist you today?\"\n",
            "      },\n",
            "      \"finish_reason\": \"stop\"\n",
            "    }\n",
            "  ],\n",
            "  \"usage\": {\n",
            "    \"prompt_tokens\": 15,\n",
            "    \"completion_tokens\": 9,\n",
            "    \"total_tokens\": 24\n",
            "  }\n",
            "}\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# we set langfuse as a callback in the prev cell\n",
        "# cohere call\n",
        "response = completion(\n",
        "  model=\"command-nightly\",\n",
        "  messages=[\n",
        "    {\"role\": \"user\", \"content\": \"Hi πŸ‘‹ - i'm cohere\"}\n",
        "  ]\n",
        ")\n",
        "\n",
        "print(response)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "2PMSLc_FziJL",
        "outputId": "1c37605e-b406-4ffc-aafd-e1983489c6be"
      },
      "execution_count": 9,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "{\n",
            "  \"object\": \"chat.completion\",\n",
            "  \"choices\": [\n",
            "    {\n",
            "      \"finish_reason\": \"stop\",\n",
            "      \"index\": 0,\n",
            "      \"message\": {\n",
            "        \"content\": \" Nice to meet you, Cohere! I'm excited to be meeting new members of the AI community\",\n",
            "        \"role\": \"assistant\",\n",
            "        \"logprobs\": null\n",
            "      }\n",
            "    }\n",
            "  ],\n",
            "  \"id\": \"chatcmpl-a14e903f-4608-4ceb-b996-8ebdf21360ca\",\n",
            "  \"created\": 1696392247.3313863,\n",
            "  \"model\": \"command-nightly\",\n",
            "  \"usage\": {\n",
            "    \"prompt_tokens\": 8,\n",
            "    \"completion_tokens\": 20,\n",
            "    \"total_tokens\": 28\n",
            "  }\n",
            "}\n"
          ]
        }
      ]
    }
  ]
}