Install the dependencies

pip install langtrace-python-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",
)

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()

Run the scripts as follows

export OPENAI_API_KEY="<YOUR OPENAI API KEY>"
python main.py

For Typescript to work, you will have to add the following line in package.json file. json "type": "module"