Spaces:
Runtime error
Runtime error
File size: 2,444 Bytes
4d6ca8c 35cce96 e6f89d5 4d6ca8c 35cce96 4d6ca8c 42cdc8f 4d6ca8c 35cce96 4d6ca8c 42cdc8f 35cce96 74dace5 35cce96 4d6ca8c 35cce96 42cdc8f 35cce96 74dace5 4d6ca8c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
import datetime
import requests
from anthropic import Anthropic, AsyncAnthropic
from dotenv import load_dotenv
from langsmith.run_helpers import traceable
from langsmith.run_trees import RunTree
from claude_space.settings import settings
load_dotenv()
@traceable(run_type="llm")
class AnthropicCustom:
def __init__(
self,
api_key,
model,
run_tree: RunTree,
max_tokens=1000,
prompt="",
):
self.api_key = api_key
self.model = model
self.max_tokens = max_tokens
self.prompt = prompt
self.run_tree = run_tree
def get_anthropic_response(self):
syncClient = Anthropic(api_key=self.api_key, timeout=5)
response = syncClient.completions.create(
prompt=self.prompt,
model=self.model,
max_tokens_to_sample=self.max_tokens,
)
self.final_response = response.completion
return response.completion
async def get_anthropic_response_async(self):
asyncClient = AsyncAnthropic(api_key=self.api_key, timeout=60)
self.final_response = ""
try:
async for line in await asyncClient.completions.create(
prompt=self.prompt,
model=self.model,
max_tokens_to_sample=self.max_tokens,
stream=True,
):
self.final_response += line.completion
yield line.completion
finally:
self.get_final_response()
def get_final_response(self):
requests.post(
url=f"{settings.LANGCHAIN_ENDPOINT}/runs",
json={
"name": "Anthropic",
"outputs": {"text": self.final_response},
"run_type": "chain",
"inputs": {"text": self.prompt},
"start_time": datetime.datetime.utcnow().isoformat(),
"end_time": datetime.datetime.utcnow().isoformat(),
"tags": [self.model, self.max_tokens],
"parent_run_id": str(self.run_tree.id),
"session_name": settings.LANGCHAIN_PROJECT,
},
headers={
"x-api-key": settings.LANGCHAIN_API_KEY,
},
)
def __str__(self):
return self.final_response
def __repr__(self):
return self.final_response
def __iter__(self):
return iter(self.final_response)
|