-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1164 from You-OpenSource/you/chat-apis
feat(you-llm): Integrate You.com conversational APIs
- Loading branch information
Showing
5 changed files
with
135 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
--- | ||
sidebar_position: 13 | ||
--- | ||
|
||
# dspy.You | ||
Wrapper around [You.com's conversational Smart and Research APIs](https://documentation.you.com/api-reference/). | ||
|
||
Each API endpoint is designed to generate conversational | ||
responses to a variety of query types, including inline citations | ||
and web results when relevant. | ||
|
||
Smart Mode: | ||
- Quick, reliable answers for a variety of questions | ||
- Cites the entire web page URL | ||
|
||
Research Mode: | ||
- In-depth answers with extensive citations for a variety of questions | ||
- Cites the specific web page snippet relevant to the claim | ||
|
||
For more information, check out the documentations at | ||
https://documentation.you.com/api-reference/. | ||
|
||
|
||
### Constructor | ||
```python | ||
You( | ||
endpoint: Literal["smart", "research"] = "smart", | ||
ydc_api_key: Optional[str] = None, | ||
) | ||
``` | ||
|
||
**Parameters:** | ||
- `endpoint`: You.com conversational endpoints. Choose from "smart" or "research" | ||
- `ydc_api_key`: You.com API key, if `YDC_API_KEY` is not set in the environment | ||
|
||
### Usage | ||
Obtain a You.com API key from https://api.you.com/. | ||
|
||
Export this key to an environment variable `YDC_API_KEY`. | ||
|
||
```python | ||
import dspy | ||
|
||
# The API key is inferred from the `YDC_API_KEY` environment variable | ||
lm = dspy.You(endpoint="smart") | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,3 +27,4 @@ | |
from .snowflake import * | ||
from .tensorrt_llm import TensorRTModel | ||
from .watsonx import * | ||
from .you import You |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import os | ||
from typing import Any, Literal, Optional | ||
|
||
import requests | ||
|
||
from dsp.modules.lm import LM | ||
|
||
SMART_ENDPOINT = "https://chat-api.you.com/smart" | ||
RESEARCH_ENDPOINT = "https://chat-api.you.com/research" | ||
|
||
|
||
class You(LM): | ||
"""Wrapper around You.com's conversational Smart and Research APIs. | ||
Each API endpoint is designed to generate conversational | ||
responses to a variety of query types, including inline citations | ||
and web results when relevant. | ||
Smart Mode: | ||
- Quick, reliable answers for a variety of questions | ||
- Cites the entire web page URL | ||
Research Mode: | ||
- In-depth answers with extensive citations for a variety of questions | ||
- Cites the specific web page snippet relevant to the claim | ||
To connect to the You.com api requires an API key which | ||
you can get at https://api.you.com. | ||
For more information, check out the documentations at | ||
https://documentation.you.com/api-reference/. | ||
Args: | ||
endpoint: You.com conversational endpoints. Choose from "smart" or "research" | ||
api_key: You.com API key, if `YDC_API_KEY` is not set in the environment | ||
""" | ||
|
||
def __init__( | ||
self, | ||
endpoint: Literal["smart", "research"] = "smart", | ||
ydc_api_key: Optional[str] = None, | ||
): | ||
super().__init__(model="you.com") | ||
self.ydc_api_key = ydc_api_key or os.environ["YDC_API_KEY"] | ||
self.endpoint = endpoint | ||
|
||
# Mandatory DSPy attributes to inspect LLM call history | ||
self.history = [] | ||
self.provider = "you.com" | ||
|
||
def basic_request(self, prompt, **kwargs) -> dict[str, Any]: | ||
headers = {"x-api-key": self.ydc_api_key} | ||
params = {"query": prompt} # DSPy `kwargs` are ignored as they are not supported by the API | ||
|
||
response = requests.post(self.request_endpoint, headers=headers, json=params) | ||
response.raise_for_status() | ||
|
||
data = response.json() | ||
|
||
# Update history | ||
self.history.append({"prompt": prompt, "response": data, "endpoint": self.endpoint}) | ||
|
||
return data | ||
|
||
@property | ||
def request_endpoint(self) -> str: | ||
if self.endpoint == "smart": | ||
return SMART_ENDPOINT | ||
return RESEARCH_ENDPOINT | ||
|
||
def __call__(self, prompt, only_completed: bool = True, return_sorted: bool = False, **kwargs) -> list[str]: | ||
response = self.request(prompt, **kwargs) | ||
return [response["answer"]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters