Skip to content

Commit

Permalink
Merge pull request #132 from jralduaveuthey/langsmith
Browse files Browse the repository at this point in the history
added langsmith code
  • Loading branch information
mruwnik committed Dec 2, 2023
2 parents 162ad10 + d94abb7 commit 51d82c8
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 3 deletions.
5 changes: 5 additions & 0 deletions api/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,8 @@ 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
LANGCHAIN_TRACING_V2="false" #set to "true" only if using langsmith
31 changes: 29 additions & 2 deletions api/main.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
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

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
Expand Down Expand Up @@ -82,7 +86,6 @@ def chat_simplified(param=''):
@app.route('/human/<id>', 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}")

Expand All @@ -94,6 +97,30 @@ def human(id):
# <a href=\"https://stampy.ai/?state=6207&question=What%20is%20%22superintelligence%22%3F\">
text = re.sub(r'<a href=\\"/\?state=(\d+.*)\\">', r'<a href=\"https://aisafety.info/?state=\1\\">', r.text)

if LANGCHAIN_API_KEY: #add to langsmith
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')

# ------------------------------------------------------------------------------
Expand Down
9 changes: 8 additions & 1 deletion api/src/stampy_chat/chat.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
from typing import Any, Callable, Dict, List

from langchain.chains import LLMChain, OpenAIModerationChain
Expand All @@ -12,12 +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 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
Expand Down
5 changes: 5 additions & 0 deletions api/src/stampy_chat/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,8 @@

### 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")
LANGCHAIN_TRACING_V2 = os.environ.get("LANGCHAIN_TRACING_V2")

0 comments on commit 51d82c8

Please sign in to comment.