From 670f36ef7917009d00bd0f0f37914588718ec91a Mon Sep 17 00:00:00 2001 From: jralduaveuthey Date: Wed, 29 Nov 2023 15:24:59 +0100 Subject: [PATCH 1/2] added langsmith code --- api/.env.example | 4 ++++ api/main.py | 31 ++++++++++++++++++++++++++++++- api/src/stampy_chat/chat.py | 7 +++++++ api/src/stampy_chat/env.py | 4 ++++ 4 files changed, 45 insertions(+), 1 deletion(-) diff --git a/api/.env.example b/api/.env.example index 0b00b8f..0296441 100644 --- a/api/.env.example +++ b/api/.env.example @@ -9,3 +9,7 @@ CHAT_DB_USER="user" CHAT_DB_PASSWORD="we all live in a yellow submarine" CHAT_DB_HOST="127.0.0.1" CHAT_DB_PORT="3306" + +LANGCHAIN_ENDPOINT="https://api.smith.langchain.com" +LANGCHAIN_API_KEY="ls_XXXXXXXXXXXX" #leave empty to not use langsmith for monitoring +LANGCHAIN_PROJECT="stampy-chat" #name of your langsmith project \ No newline at end of file diff --git a/api/main.py b/api/main.py index d5bb0e8..f04d68f 100644 --- a/api/main.py +++ b/api/main.py @@ -1,6 +1,10 @@ import dataclasses +import requests +import datetime +import uuid import json import re +import os from flask import Flask, jsonify, request, Response, stream_with_context from flask_cors import CORS, cross_origin @@ -82,7 +86,6 @@ def chat_simplified(param=''): @app.route('/human/', methods=['GET']) @cross_origin() def human(id): - import requests r = requests.get(f"https://aisafety.info/questions/{id}") logging.info(f"clicked followup '{json.loads(r.text)['data']['title']}': https://stampy.ai/?state={id}") @@ -94,6 +97,32 @@ def human(id): # text = re.sub(r'', r'', r.text) + LANGCHAIN_API_KEY = os.environ["LANGCHAIN_API_KEY"] + if LANGCHAIN_API_KEY: #add to langsmith + LANGCHAIN_PROJECT = os.environ["LANGCHAIN_PROJECT"] + run_id = str(uuid.uuid4()) + requests.post( + "https://api.smith.langchain.com/runs", + json={ + "id": run_id, + "name": "aisafety.info/question", + "run_type": "chain", + "start_time": datetime.datetime.utcnow().isoformat(), + "session_name": LANGCHAIN_PROJECT, + "inputs": {"text": f"clicked followup '{json.loads(r.text)['data']['title']}': https://stampy.ai/?state={id}"}, + }, + headers={"x-api-key": LANGCHAIN_API_KEY}, + ) + + requests.patch( + f"https://api.smith.langchain.com/runs/{run_id}", + json={ + "outputs": {"my_output": text}, + "end_time": datetime.datetime.utcnow().isoformat(), + }, + headers={"x-api-key": LANGCHAIN_API_KEY}, + ) + return Response(text, mimetype='application/json') # ------------------------------------------------------------------------------ diff --git a/api/src/stampy_chat/chat.py b/api/src/stampy_chat/chat.py index d5774f2..6ddf56e 100644 --- a/api/src/stampy_chat/chat.py +++ b/api/src/stampy_chat/chat.py @@ -1,3 +1,4 @@ +import os from typing import Any, Callable, Dict, List from langchain.chains import LLMChain, OpenAIModerationChain @@ -18,6 +19,12 @@ from stampy_chat.followups import StampyChain from stampy_chat.citations import make_example_selector +from langsmith import Client +if os.environ.get("LANGCHAIN_API_KEY"): + os.environ['LANGCHAIN_TRACING_V2'] = "true" + client = Client() +else: + os.environ['LANGCHAIN_TRACING_V2'] = "false" #necessary in case the user sets is a true in the .env file but has not entered an api key class ModerationError(ValueError): pass diff --git a/api/src/stampy_chat/env.py b/api/src/stampy_chat/env.py index c0df97d..8142d0b 100644 --- a/api/src/stampy_chat/env.py +++ b/api/src/stampy_chat/env.py @@ -47,3 +47,7 @@ ### Local testing helpers ### REMOTE_CHAT_INSTANCE = os.environ.get("REMOTE_CHAT_INSTANCE", "https://chat.stampy.ai:8443") + +### Langsmith ### +LANGCHAIN_API_KEY = os.environ.get("LANGCHAIN_API_KEY") +LANGCHAIN_PROJECT = os.environ.get("LANGCHAIN_PROJECT") \ No newline at end of file From d94abb7d9908728577beb7377a1749368d2860e1 Mon Sep 17 00:00:00 2001 From: jralduaveuthey Date: Wed, 29 Nov 2023 17:07:58 +0100 Subject: [PATCH 2/2] changed logic env variables --- api/.env.example | 3 ++- api/main.py | 4 +--- api/src/stampy_chat/chat.py | 12 ++++++------ api/src/stampy_chat/env.py | 3 ++- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/api/.env.example b/api/.env.example index 0296441..1ee88ea 100644 --- a/api/.env.example +++ b/api/.env.example @@ -12,4 +12,5 @@ CHAT_DB_PORT="3306" LANGCHAIN_ENDPOINT="https://api.smith.langchain.com" LANGCHAIN_API_KEY="ls_XXXXXXXXXXXX" #leave empty to not use langsmith for monitoring -LANGCHAIN_PROJECT="stampy-chat" #name of your langsmith project \ No newline at end of file +LANGCHAIN_PROJECT="stampy-chat" #name of your langsmith project +LANGCHAIN_TRACING_V2="false" #set to "true" only if using langsmith diff --git a/api/main.py b/api/main.py index f04d68f..e35ac43 100644 --- a/api/main.py +++ b/api/main.py @@ -10,7 +10,7 @@ from flask_cors import CORS, cross_origin from stampy_chat import logging -from stampy_chat.env import PINECONE_INDEX, FLASK_PORT +from stampy_chat.env import PINECONE_INDEX, FLASK_PORT, LANGCHAIN_API_KEY, LANGCHAIN_PROJECT from stampy_chat.settings import Settings from stampy_chat.chat import run_query from stampy_chat.callbacks import stream_callback @@ -97,9 +97,7 @@ def human(id): # text = re.sub(r'', r'', r.text) - LANGCHAIN_API_KEY = os.environ["LANGCHAIN_API_KEY"] if LANGCHAIN_API_KEY: #add to langsmith - LANGCHAIN_PROJECT = os.environ["LANGCHAIN_PROJECT"] run_id = str(uuid.uuid4()) requests.post( "https://api.smith.langchain.com/runs", diff --git a/api/src/stampy_chat/chat.py b/api/src/stampy_chat/chat.py index 6ddf56e..c14bd33 100644 --- a/api/src/stampy_chat/chat.py +++ b/api/src/stampy_chat/chat.py @@ -13,18 +13,18 @@ from langchain.pydantic_v1 import Extra from langchain.schema import BaseMessage, ChatMessage, PromptValue, SystemMessage -from stampy_chat.env import OPENAI_API_KEY, COMPLETIONS_MODEL +from stampy_chat.env import OPENAI_API_KEY, COMPLETIONS_MODEL, LANGCHAIN_API_KEY, LANGCHAIN_TRACING_V2 from stampy_chat.settings import Settings from stampy_chat.callbacks import StampyCallbackHandler, BroadcastCallbackHandler, LoggerCallbackHandler from stampy_chat.followups import StampyChain from stampy_chat.citations import make_example_selector from langsmith import Client -if os.environ.get("LANGCHAIN_API_KEY"): - os.environ['LANGCHAIN_TRACING_V2'] = "true" - client = Client() -else: - os.environ['LANGCHAIN_TRACING_V2'] = "false" #necessary in case the user sets is a true in the .env file but has not entered an api key + +if LANGCHAIN_TRACING_V2 == "true": + if not LANGCHAIN_API_KEY: + raise Exception("Langsmith tracing is enabled but no api key was provided. Please set LANGCHAIN_API_KEY in the .env file.") + client = Client() class ModerationError(ValueError): pass diff --git a/api/src/stampy_chat/env.py b/api/src/stampy_chat/env.py index 8142d0b..4495eb8 100644 --- a/api/src/stampy_chat/env.py +++ b/api/src/stampy_chat/env.py @@ -50,4 +50,5 @@ ### Langsmith ### LANGCHAIN_API_KEY = os.environ.get("LANGCHAIN_API_KEY") -LANGCHAIN_PROJECT = os.environ.get("LANGCHAIN_PROJECT") \ No newline at end of file +LANGCHAIN_PROJECT = os.environ.get("LANGCHAIN_PROJECT") +LANGCHAIN_TRACING_V2 = os.environ.get("LANGCHAIN_TRACING_V2")