Install the dependencies
pip install langtrace-python-sdk openai
npm i @langtrase/typescript-sdk openai
For typescript, add a package.json as follows:
{
"dependencies": {
"@langtrase/typescript-sdk": "^3.3.2",
"openai": "^4.50.0"
}
}
Following are the parameters that you need to initialize the SDK:
from langtrace_python_sdk import langtrace
langtrace.init(
api_key="<YOUR API KEY>",
api_host="http://localhost:3000/api/trace",
)
import * as Langtrace from "@langtrase/typescript-sdk";
Langtrace.init({
api_key: "<YOUR API KEY>",
batch: false,
api_host: "http://localhost:3000/api/trace",
});
Here we are using http://localhost:3000/api/trace as the API host. You can replace it with your own DNS or IP address.
Sample Code
Following is a ready-to-run code snippet that you can use to test the SDK with your local setup:
Make Sure to pip or npm install the required packages before running the code.
from langtrace_python_sdk import langtrace
from langtrace_python_sdk.utils.with_root_span import with_langtrace_root_span
from openai import OpenAI
langtrace.init(
api_key="<YOUR API KEY>",
api_host="http://localhost:3000/api/trace",
)
@with_langtrace_root_span()
def example():
client = OpenAI()
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{
"role": "system",
"content": "How many states of matter are there?"
}
],
)
print(response.choices[0].message.content)
example()
import * as Langtrace from "@langtrase/typescript-sdk";
import OpenAI from "openai";
Langtrace.init({
api_key: "<YOUR API KEY>",
batch: false,
api_host: "http://localhost:3000/api/trace",
instrumentations: {
openai: OpenAI,
},
});
const openai = new OpenAI();
async function example() {
const completion = await openai.chat.completions.create({
model: "gpt-3.5-turbo",
messages: [
{
role: "system",
content: "How many states of matter are there?",
},
],
});
console.log(completion.choices[0]);
}
example().then(() => {
console.log("done");
});
Run the scripts as follows
export OPENAI_API_KEY="<YOUR OPENAI API KEY>"
python main.py
export OPENAI_API_KEY="<YOUR OPENAI API KEY>"
node main.js
For Typescript to work, you will have to add the following line in
package.json file. json "type": "module"