Skip to content

Commit

Permalink
Fix merge commit
Browse files Browse the repository at this point in the history
  • Loading branch information
karthikscale3 committed Jun 13, 2024
2 parents dca1611 + 3f22fe3 commit b9f5e83
Show file tree
Hide file tree
Showing 28 changed files with 2,200 additions and 211 deletions.
129 changes: 20 additions & 109 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ The traces generated by Langtrace adhere to [Open Telemetry Standards(OTEL)](htt

---

## SDK Repositories

- [Langtrace Typescript SDK](https://github.com/Scale3-Labs/langtrace-typescript-sdk)
- [Langtrace Python SDK](https://github.com/Scale3-Labs/langtrace-python-sdk)
- [Semantic Span Attributes](https://github.com/Scale3-Labs/langtrace-trace-attributes)

---

## Getting Started

### Langtrace Cloud ☁️
Expand All @@ -34,7 +42,7 @@ To use the managed SaaS version of Langtrace, follow the steps below:
4. In your application, install the Langtrace SDK and initialize it with the API key you generated in the step 3.
5. The code for installing and setting up the SDK is shown below:

### If your application is built using **typescript/javascript**
### If your application is built using typescript/javascript

```typescript
npm i @langtrase/typescript-sdk
Expand All @@ -52,7 +60,7 @@ import * as Langtrace from "@langtrase/typescript-sdk"; // Must precede any llm
LangTrace.init(); // LANGTRACE_API_KEY as an ENVIRONMENT variable
```

### If your application is built using **python**
### If your application is built using python

```python
pip install langtrace-python-sdk
Expand All @@ -79,7 +87,7 @@ To run the Langtrace locally, you have to run three services:
- Clickhouse database

> [!IMPORTANT]
> Checkout [documentation](https://docs.langtrace.ai/hosting/overview) for various deployment options and configurations.
> Checkout our [documentation](https://docs.langtrace.ai/hosting/overview) for various deployment options and configurations.
Requirements:

Expand All @@ -98,16 +106,6 @@ docker compose up

The application will be available at `http://localhost:3000`.

> [!NOTE]
> if you wish to build the docker image locally and use it, run the docker compose up command with the `--build` flag.
> [!TIP]
> to manually pull the docker image from [docker hub](https://hub.docker.com/r/scale3labs/langtrace-client/tags), run the following command:
>
> ```bash
> docker pull scale3labs/langtrace-client:latest
> ```
#### Take down the setup

To delete containers and volumes
Expand All @@ -118,100 +116,6 @@ docker compose down -v

`-v` flag is used to delete volumes

#### Useful Ops Commands

The following are some commands that may come handy during setup and debugging.

<details>
<summary>Connecting to postgres db</summary>

```bash
docker exec -it langtrace-postgres psql --dbname=langtrace --username=ltuser --password
```

</details>

<details>
<summary>Connecting to clickhouse server</summary>

```bash
docker exec -it langtrace-clickhouse clickhouse-client
```

</details>

<details>
<summary>Checking langtrace client app logs</summary>

```bash
docker logs langtrace
```

If you want to follow the logs

```bash
docker logs -f langtrace
```

</details>

<details>
<summary>Running prisma schema apply command</summary>

```bash
docker exec -it langtrace npm run create-tables
```

</details>

#### Common issues for local setup

<details>
<summary>Table not found error OR Column not found error</summary>
Its likely that schema is not applied to the database or the schema is not in sync with the database. To fix this, run the following command:

```bash
docker exec -it langtrace npm run create-tables
```

</details>

<details>
<summary>Prisma schema not in sync with database</summary>
If you have made changes to the prisma schema and want to apply the changes to the database, run the following command:

```bash
docker exec -it langtrace npm run create-tables
```

</details>

<details>
<summary>Docker compose failing to setup with `Additional property required is not allowed` errors</summary>
Its likely that you are using an older version of docker-compose. Update docker-compose to the latest version.

Certain docker compose schema used in this project are only supported in newer versions of docker-compose.

Either you **update the docker compose version** OR **remove the depends_on property** that is causing the error.

</details>

<details>
<summary>Clickhouse server not starting</summary>
If clickhouse server is not starting, it is likely that the port 8123 is already in use. You can change the port in the docker-compose file.
</details>

<br/>
Install the langtrace SDK in your application by following the same instructions under the Langtrace Cloud section above for sending traces to your self hosted setup.

---

## SDK Repositories

- [Langtrace Typescript SDK](https://github.com/Scale3-Labs/langtrace-typescript-sdk)
- [Langtrace Python SDK](https://github.com/Scale3-Labs/langtrace-python-sdk)
- [Semantic Span Attributes](https://github.com/Scale3-Labs/langtrace-trace-attributes)

---

## Supported integrations
Expand All @@ -230,6 +134,7 @@ Langtrace automatically captures traces from the following vendors:
| Pinecone | Vector Database | :white_check_mark: | :white_check_mark: |
| ChromaDB | Vector Database | :white_check_mark: | :white_check_mark: |
| QDrant | Vector Database | :x: | :white_check_mark: |
| Weaviate | Vector Database | :white_check_mark: | :white_check_mark: |

---

Expand All @@ -252,6 +157,12 @@ We welcome contributions to this project. To get started, fork this repository a

---

## Langtrace Star History

## [![Langtrace Star History Chart](https://api.star-history.com/svg?repos=Scale3-Labs/langtrace&type=Timeline)](https://star-history.com/#Scale3-Labs/langtrace&Timeline)

---

## Security

To report security vulnerabilites, email us at <security@scale3labs.com>. You can read more on security [here](https://github.com/Scale3-Labs/langtrace/blob/development/SECURITY.md).
Expand All @@ -268,7 +179,7 @@ To report security vulnerabilites, email us at <security@scale3labs.com>. You ca
## Frequently Asked Questions

**1. Can I self host and run Langtrace in my own cloud?**
Yes, you can absolutely do that. Follow the self hosting setup instructions laid out above.
Yes, you can absolutely do that. Follow the self hosting setup instructions in our [documentation](https://docs.langtrace.ai/hosting/overview).

**2. What is the pricing for Langtrace cloud?**
Currently, we are not charging anything for Langtrace cloud and we are primarily looking for feedback so we can continue to improve the project. We will inform our users when we decide to monetize it.
Expand All @@ -277,7 +188,7 @@ Currently, we are not charging anything for Langtrace cloud and we are primarily
Langtrace uses NextJS for the frontend and APIs. It uses PostgresDB as a metadata store and Clickhouse DB for storing spans, metrics, logs and traces.

**4. Can I contribute to this project?**
Absolutely! We love developers and welcome contributions. Get involved early by joining our slack workspace.
Absolutely! We love developers and welcome contributions. Get involved early by joining our [Discord Community](https://discord.langtrace.ai/).

**5. What skillset is required to contribute to this project?**
Programming Languages: Typescript and Python.
Expand Down
13 changes: 11 additions & 2 deletions app/api/metrics/latency/trace/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export async function GET(req: NextRequest) {
const lastNHours = parseInt(
req.nextUrl.searchParams.get("lastNHours") || "168"
);
const userId = req.nextUrl.searchParams.get("userId") || "";
const model = req.nextUrl.searchParams.get("model") || "";
if (!projectId) {
return NextResponse.json(
JSON.stringify({ message: "projectId is required" }),
Expand All @@ -26,11 +28,18 @@ export async function GET(req: NextRequest) {

const traceService = new TraceService();
const totalTracesPerHour: any =
await traceService.GetTotalTracePerHourPerProject(projectId, lastNHours); // last 7 days in hours
await traceService.GetTotalTracePerHourPerProject(
projectId,
lastNHours,
userId,
model
);
const { averageLatencies, p99Latencies, p95Latencies } =
await traceService.GetAverageTraceLatenciesPerHourPerProject(
projectId,
lastNHours
lastNHours,
userId,
model
);
return NextResponse.json(
{
Expand Down
22 changes: 20 additions & 2 deletions app/api/metrics/usage/cost/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,31 @@ export async function GET(req: NextRequest) {
const lastNHours = parseInt(
req.nextUrl.searchParams.get("lastNHours") || "168"
);
const userId = req.nextUrl.searchParams.get("userId") || "";
const model = req.nextUrl.searchParams.get("model") || "";

const traceService = new TraceService();
const total = await traceService.GetTokensCostPerProject(projectId);
const cost = await traceService.GetTokensCostPerHourPerProject(
projectId,
lastNHours
lastNHours,
userId,
model
);
const total = {
total: cost.reduce(
(acc: any, curr: { total: any }) => acc + curr.total,
0
),
input: cost.reduce(
(acc: any, curr: { input: any }) => acc + curr.input,
0
),
output: cost.reduce(
(acc: any, curr: { output: any }) => acc + curr.output,
0
),
};

return NextResponse.json(
{
cost,
Expand Down
10 changes: 8 additions & 2 deletions app/api/metrics/usage/span/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export async function GET(req: NextRequest) {
const lastNHours = parseInt(
req.nextUrl.searchParams.get("lastNHours") || "168"
);
const userId = req.nextUrl.searchParams.get("userId") || "";
if (!projectId) {
return NextResponse.json(
JSON.stringify({ message: "projectId is required" }),
Expand All @@ -27,9 +28,14 @@ export async function GET(req: NextRequest) {
const traceService = new TraceService();
const spans: any = await traceService.GetTotalSpansPerHourPerProject(
projectId,
lastNHours
lastNHours,
userId
);
const total = spans.reduce(
(acc: number, curr: { spanCount: string }) =>
acc + Number(curr.spanCount),
0
);
const total: any = await traceService.GetTotalSpansPerProject(projectId);
return NextResponse.json(
{
spans,
Expand Down
22 changes: 19 additions & 3 deletions app/api/metrics/usage/token/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export async function GET(req: NextRequest) {
const lastNHours = parseInt(
req.nextUrl.searchParams.get("lastNHours") || "168"
);
const userId = req.nextUrl.searchParams.get("userId") || "";
const model = req.nextUrl.searchParams.get("model") || "";

if (!projectId) {
return NextResponse.json(
Expand All @@ -24,13 +26,27 @@ export async function GET(req: NextRequest) {
}
);
}

const traceService = new TraceService();
const total = await traceService.GetTokensUsedPerProject(projectId);
const usage = await traceService.GetTokensUsedPerHourPerProject(
projectId,
lastNHours
lastNHours,
userId,
model
);
const total = {
totalTokens: usage.reduce(
(acc: any, curr: { totalTokens: any }) => acc + curr.totalTokens,
0
),
inputTokens: usage.reduce(
(acc: any, curr: { inputTokens: any }) => acc + curr.inputTokens,
0
),
outputTokens: usage.reduce(
(acc: any, curr: { outputTokens: any }) => acc + curr.outputTokens,
0
),
};
return NextResponse.json(
{
usage,
Expand Down
13 changes: 11 additions & 2 deletions app/api/metrics/usage/trace/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export async function GET(req: NextRequest) {
const lastNHours = parseInt(
req.nextUrl.searchParams.get("lastNHours") || "168"
);
const userId = req.nextUrl.searchParams.get("userId") || "";
const model = req.nextUrl.searchParams.get("model") || "";

if (!projectId) {
return NextResponse.json(
Expand All @@ -28,9 +30,16 @@ export async function GET(req: NextRequest) {
const traceService = new TraceService();
const traces: any = await traceService.GetTotalTracePerHourPerProject(
projectId,
lastNHours
lastNHours,
userId,
model
);
const total: any = await traceService.GetTotalTracesPerProject(projectId);
const total = traces.reduce(
(acc: number, curr: { traceCount: string }) =>
acc + Number(curr.traceCount),
0
);

return NextResponse.json(
{
traces,
Expand Down
37 changes: 37 additions & 0 deletions app/api/model-ids/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { authOptions } from "@/lib/auth/options";
import { TraceService } from "@/lib/services/trace_service";
import { getServerSession } from "next-auth";
import { redirect } from "next/navigation";
import { NextRequest, NextResponse } from "next/server";

export async function GET(req: NextRequest) {
const session = await getServerSession(authOptions);

if (!session || !session.user) {
redirect("/login");
}

try {
const projectId = req.nextUrl.searchParams.get("projectId") as string;

if (!projectId) {
return NextResponse.json(
{ error: "Please provide a projectId" },
{ status: 400 }
);
}

const traceService = new TraceService();
const models = await traceService.GetModelsInProject(projectId);
return NextResponse.json(
{ models },
{
status: 200,
}
);
} catch (error) {
return NextResponse.json(JSON.stringify({ error }), {
status: 400,
});
}
}
Loading

0 comments on commit b9f5e83

Please sign in to comment.